carlos88
carlos88

Reputation: 29

How do I remove text using sed?

For instance let say I have a text file:

worker1, 0001, company1
worker2, 0002, company2
worker3, 0003, company3

How would I use sed to take the first 2 characters of the first column so "wo" and remove the rest of the text and attach it to the second column so the output would look like this:

wo0001,company1
wo0002,company2
wo0003,company3

Upvotes: 0

Views: 111

Answers (1)

Freddy
Freddy

Reputation: 4718

$ sed -E 's/^(..)[^,]*, ([^,]*,) /\1\2/' file
wo0001,company1
wo0002,company2
wo0003,company3
  • s/ begin substitution
  • ^(..) match the first two characters at the beginning of the line, captured in a group
  • [^,]* match any amount of non-comma characters of the first column
  • , match a comma and a space character
  • ([^,]*,) match the second field and comma captured in a group (any amount of non-comma characters followed by a comma)
  • match the next space character
  • /\1\2/ replace with the first and second capturing group

Upvotes: 1

Related Questions