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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed Question

Status
Not open for further replies.

poloman

MIS
Apr 28, 2001
1
0
0
US
Hello Everyone,

I'm new to Unix and the book I'm using doesnt seem to be providing me the answer I'm looking for. Or perhaps I'm too blurry eyed at this point to see it.

What I need is a Sed command in a script I'm working on that will...

1. Look for a string variable $ItemNum in a data file.
2. When it finds an instance of $ItemNum - Delete that entire line of text where $ItemNum resides.

The best I've managed so far is to delete the entire data file on accident (a copy is safely in another directory of course).

Thank you for your time and any tips.

Regards,
Poloman
 
If the string you search for is unique in the file, it will be easier to use grep -v string file. This will give (to stdout) all the lines EXCEPT the line containing 'string'.
grep -v string file > new.file
will put everything except the line containing 'string' in the new file which you can then rename to the original (mv new.file file) .

If I take your statements verbatum, then all you need to use is grep -v . You do not even need 'sed' .

sed, by itself, may not allow you to remove the entire line unless: after locating the line you create another sed command which substitutes the entire line with null. This will give you a blank line unless you cleaverly remove the newline character (that's another discussion).

You can use 'ed' (a line editor) run from a script, to locate a string and do edits from that point, like removing the entire line. I have done such things within a shell script.

Last but not least, awk may give you more capability to do these things than sed but I don't know it that well.

From what you said, grep -v will server you better than 'sed'.

Hope that helps.
 
sed /$ItemNum/d file1>file2

will give you the same result as

grep -v $ItemNum file1>file2
 
A solution is :

cat file | sed '/.*'$ItemNum'.*/d' > file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top