Home > Software design >  Sed to remove more than 2 words in single line
Sed to remove more than 2 words in single line

Time:08-25

I'm asking your help, I want to remove words 'TAGS' , 'Owner' , 'QA' from the txt file . in single sed command , example sed -e 's/\TAGS//g' test.txt and add the email address example [email protected] to the end of the line example postgres vp-postgres None default:postgres-13 True stopped db.m6g.large 13.4 vpc-95525af1 [email protected]

postgres    vp-postgres None    default:postgres-13 True    stopped db.m6g.large    13.4    vpc-95525af1
TAGS    Owner   [email protected]
TAGS        QA
TAGS    autostop    yes

CodePudding user response:

Using GNU sed

$ sed -E ':a;N;s/(\n.*)?( [[:alpha:]] @.*)\n.*/\2/;ba' input_file
postgres    vp-postgres None    default:postgres-13 True    stopped db.m6g.large    13.4    vpc-95525af1 [email protected]

CodePudding user response:

One option could be first matching the line that starts with postgress.

Then pull the next line in the pattern space, and replace the leading TAGS or Owner or QA.

sed -E '/^postgres/{N;s/\n([[:blank:]]*(TAGS|Owner|QA))*//}' file test.txt

Output

postgres    vp-postgres None    default:postgres-13 True    stopped db.m6g.large    13.4    vpc-95525af1   [email protected]
TAGS        QA
TAGS    autostop    yes

If you only want that modified line as an output:

sed -En '/^postgres/{N;s/\n([[:blank:]]*(TAGS|Owner|QA))*//;p}' file

Output

postgres    vp-postgres None    default:postgres-13 True    stopped db.m6g.large    13.4    vpc-95525af1   [email protected]

CodePudding user response:

You can perform multiple commands in a single invocation of sed:

sed -e s/TAGS//g -e s/Owner//g -e s/QA//g -e '/^postgres/s/$/ [email protected]/' test.txt
  • Related