- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: script help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2004 10:13 AM
08-04-2004 10:13 AM
I use a script that does some clean up when users are removed from the system, and sends an email to various people with info. Now we have a bunch of single column lists that these usernames might be in also. I'm looking for a way to remove the names from those lists also, automagically!
I can find the files the name is in with:
find /dir/*.txt -exec grep -l 'username' {} \;
but can't think of a way to chain that with the removal of the username. I thought of piping that through a "grep -v 'username' > something" and overwriting the same files so I don't have to do a 'mv filename.tmp filename', etc, but just can't get it.
It of course doesn't have to be one line, but that's more challanging. :)
Thanks for any assistance!
CH
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2004 05:50 PM
08-04-2004 05:50 PM
Re: script help
sed -ne '/[^username]/p' < file > file.new && mv file.new file
Awk:
awk '!/^username$/' < file > file.new && mv file.new file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2004 06:14 PM
08-04-2004 06:14 PM
Re: script help
I rarely do that, because there is no recycle bin in Linux.
I redirect the find to a file and:
while read -r filename
do
mv $filename $filename.dat
done < input filelist
Thats a good script device and follow on.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2004 06:39 PM
08-04-2004 06:39 PM
Re: script help
--- test file ---
user1
bye for now
----------------
echo `sed -e '/^user1/!d' testfile` >testfile
To simultaneously do it,
find $PWD/* -exec grep -l 'user1' {} \; | awk '{ print "echo `sed -e '/[^user1$]/\!d' "$1"` > "$1 }' | sh
It will change the contents of every file listed out there without user1 in that
Use user1 without username.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2004 03:36 AM
08-05-2004 03:36 AM
SolutionIt assumes data in the Megabyte range, not Gigabytes as it sucks up the files.
Should be close to your needs.
Only changes the files that need changing.
Only reads the files once.
Renames to old files away, just in case.
$test = shift @ARGV;
die "Must provide line to look for" unless $test;
$dir = shift @ARGV;
foreach $file (<${dir}*.txt>) {
$files++;
open (F, "< $file");
while (
if (/^$test$/) {
$need_new = 1;
$changes++;
} else {
push @lines, $_;
}
}
close (F);
if ($need_new) {
$need_new = 0;
rename $file, $file . ".old";
open (F, "> $file") or die "Can not create new file $file";
foreach (@lines) { print F $_ };
close (F);
undef @lines;
}
}
print "$changes lines removed from $files files.\n";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2004 10:24 AM
08-13-2004 10:24 AM
Re: script help
I was able to implement Hein's solution without issue, and is working as intended. I also liked Muthukumar's, what I was initially looking for, but for some reason I couldn't get my username var to pass into the sed portion. Works great on the command line, but not in my script. Have to play with it more and figure it out, but it's a great one liner!
Thanks all!
CH