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

multiple sed's

Status
Not open for further replies.

Igaduma

Technical User
Nov 23, 2001
322
BE
Hello Guru's

I read over a file many times to change certain patterns with another, predefined pattern, in this case, an empty "".
Really just removing things I don't need.
But, it involves reading the file in over & over again applying sed to a certain pattern.
Somehow I think there must be a way to do grab many patterns and all replace them with an "" in a more intelligent manner.

So, this is what the file might look like:

$cat input_file
one two three four five six
two three four five seven

If I now have to remove "one" and "seven" from the source file, this is how I do it quick & dirty:

$sed 's/one//g' input_file > output_file
$sed 's/seven//g' output_file > output2_file

Obviously this works, but it involves scanning a file 2 times.
How can one define a range of patterns to be all replaced by a fixed pattern (in this case "") in 'just one go' over the file ?

Thanks,
Iga
 
Hi

Whis is what you want ?
Code:
sed 's/one//g;s/seven//' input_file > output_file
[gray]# or[/gray]
sed 's/\(one\|seven\)//' input_file > output_file
[gray]# or[/gray]
sed -r 's/(one|seven)//' input_file > output_file
Note, that if there are more commands, is better to put them into a [tt]sed[/tt] script file, then execute it.

Feherke.
 
Bottom line:
man sed

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
ok, I was a little too quick to
"post it, forget it and receive reply and answers"

But, yes, I must read the man page for sed.
\personal note:RTFM

Thanks for making me humble again :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top