user3829086
user3829086

Reputation: 363

how to remove the multiple word and corresponding line of file using shell script

I have a text file having fields with delimited by ',' symbol. I want to delete the lines having patters like 'S' and 'A'.

For example, say my file data is like :

S,F,T,Y
B,I,J,O
S,O,L,H
R,I,O,P
A,H,K,L
U,I,O,P
A,I,S,U

I have tried code for single pattern,Please tell me how to use both patterns in this command:

sed -i '/S/d' 'file.txt'

Upvotes: 0

Views: 84

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207465

@zhujs has the appropriate solution for your example, but for future reference, you can also specify multiple commands to sed using more than one -e

sed -e '/A/d' -e '/D/d' file

Upvotes: 0

zhujs
zhujs

Reputation: 553

Try this command, you should escape the '|' character, which represents the alternation. (A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B)

 sed -e '/S\|A/d' file.

or

 sed -r -e '/S|A/d' file.

Upvotes: 2

julienc
julienc

Reputation: 20315

This is a working solution using grep (I'm not very familiar with sed unfortunately...):

grep -vE 'S|A' input.txt > output.txt

It will write in output.txt all the lines of input.txt which do not contain 'A' or 'S'.

Output:

B,I,J,O
R,I,O,P
U,I,O,P

Upvotes: 0

Related Questions