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!

[?] remove

Status
Not open for further replies.

shyg

Technical User
Jul 12, 2004
20
US
Problem:
--------
I am trying to remove a userid and name from a file once the user has specified the userid or name to remove.

Sample Code:
------------
#!/bin/sh
echo Enter a userid or name to search for and remove:
read wrd
grep -w $wrd ~/test | cut $wrd

Notes:
------
Im using grep -w cause it returns the whole line containing both the usrid and full name depending on what the user searchs by. I wanted to pipe this through cut but I guess cut doesnt work on files in this way.
 
Try something like this:
sed -e "`grep -n -w $wrd ~/test | awk -F':' '{printf \"%dd;\",$1}'`" ~/test

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
@PHV - thx but your solution didnt work for me
 
What is the output of this command ?
grep -n -w $wrd ~/test

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Enter a userid or name to search for and remove:
britm
1:britm Brittney Morris

 
So, the script I posted should output the contents of ~/test without the first line, ie with userid removed.
your solution didnt work for me
Can you please be more specific on the unexpected behaviour ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Ok when I run this and search by key word "Brittney", "Morris", or "britm" nothing is removed

Sample Code:
------------
#!/bin/sh
echo Enter a userid or name to search for and remove:
read wrd
sed -e "`grep -n -w $wrd ~/test | awk -F':' '{printf \"%dd;\",$1}'`" ~/test
 
All the test file is displayed, even the first line ?
To remove the line you have to do something like this:
sed -e "`grep -n -w $wrd ~/test | awk -F':' '{printf \"%dd;\",$1}'`" ~/test >tmp.$$ && mv tmp.$$ ~/test

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top