Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Removal

Status
Not open for further replies.

reason1000

IS-IT--Management
Feb 1, 2002
38
US
Hello,

Anyone have any example code on how to remove a string from a test file? I would like to be able to remove old uers accounts from my Linux server alias file.

Thanks,
Rod
 
That should have read "how to remove a string from a text file" not a test file.
 
Maybe this helps?

testfile
Code:
aaa:123:456
bbb:123:456
ccc:123:456

Code:
perl -i -ple 's/^bbb:/:/' testfile

now, testfile contains:
Code:
aaa:123:456
:123:456
ccc:123:456

That answers your "example code on how to remove a string from a test file".

But your example makes it sound like you want to remove the line that contains a certain user, not just the username?

In that case:
Code:
perl -i -ne 'print unless(/^bbb:/)' testfile

testfile contains (from the original):
Code:
aaa:123:456
ccc:123:456

All of this, of course, depends on the format of your text file... I just gave an example of a colon delimited file where the userid is the first field. Post back if you have further questions or if you would like to clarify more details.
 
Thanks for the help! A combination of both command line examples did the trick. I have both lines that begin with a user account name and lines that have the same user account name in the middle or end, that need to be removed.

Here's the line I'm using to do this:

Code:
system ("perl -pe 's/$useracct,//g' $file | perl -i -ne 'print unless (/^$useracct/)'");

I then direct standard output to where I want it to go such as a new file. I am still playing with different ideas and might do an edit-in-place, backing up the original file, of course.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top