0

I want to remove all data no matter what it is in between 2 words. The two words are user and Gecko) , exactly as it appears.

For example:

abcd: efgh user jfslkdj ajskdlfj askldjf Gecko) print
ijkl: mnop user fjskdf sdfjkdf skdjf sdkfj Gecko) second

Should apprear as:
abcd: efgh print
ijkl: mnop second

Please let me know if this is possible.

So far, this is what I tried and I know I am far off:

sed 's/user*Gecko)//g'

2

You can modify your command like:

sed 's/user.*Gecko)//g'

It will remove the substring user.....Gecko).

You can use:

sed -Ee "s/(.*)(user.*Gecko\))(.*)/\1\3/g" filename

It will just print first and third field and will neglect second field which contains everything between user and Gecko) including both.

If you are sure that user is not starting word and Gecko) is not last word, then you can use:

sed -Ee "s/(.*) (user.*Gecko\)) (.*)/\1 \3/g" filename

this one is more accurate as it will only replace words separated by space.

  • The g is not needed at the end of the s command as there can't be more than one match per line. If the line is XXX user YYY Gecko) XXX user YYY Gecko) then the pattern would match from the first user to the last Gecko). – Kusalananda Mar 11 at 17:52
  • And of course you don't need to capture the second field if you are not going to use it, so sed -Ee 's/(.*) user.*Gecko\) (.*)/\1 \2/' filename. Note if there is more than one user in the line before the Gecko) then this approach will remove from the last user, whilst the s/user.*Gekco)// will remove from the first one. – icarus Mar 11 at 19:40
  • Worked like a charm, you got the best answer for that one, thanks. I have another question. Say some lines say Chrome XXXX and other lines say Chrome XXXX OpenFin XXXX. How do I remove the Chrome XXXX only on the lines that has it with OpenFin XXXX so that OpenFin XXXX can display by itself? – Ibrahim A Mar 11 at 20:40
  • @IbrahimA sed -Ee '/OpenFin XXXX/s/Chrome.*?XXXX//' – icarus Mar 13 at 13:57
0

I tried with below command and it worked fine

awk '{print $1,$2,$NF}'  filename

output

abcd: efgh print
ijkl: mnop second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.