0

I have one text file which consists of group of contact numbers with names like below.

   sam=12345,john=98765,bruce=67890,jack=54321,nick=23456,noah=67894,vision=65432,thor=98768,tony=89769,cap=90878

I want to delete a particular contact nick. I can give a input as contact number. By using "sed" i know only to replace the contact number with blank "sed 's/23456//g' new.txt"but name still exists as shown below for nick it is causing error in the backend.

sam=12345,john=98765,bruce=67890,jack=54321,nick=,noah=67894,vision=65432,thor=98768,tony=89769,cap=90878

Stuck as above. I want to delete along with name without changing the format(name=number,name=number,and so..)by providing only number as input.

  • 1
    You'll need to remove the name too, which is some non-comma characters followed by an equal sign followed by the digits, followed by a comma. Can you figure out the regular expression given that hint? – glenn jackman Dec 22 '18 at 19:43
  • in my scenario i need to use only numbers as input because names are not proper (most of the case misspelled and typo errors as well) so only – naren viru Dec 22 '18 at 19:45
  • 1
    according to your file format, the name cannot contain a comma. My question remains. How do you write "some non-comma characters" as a regular expression? – glenn jackman Dec 22 '18 at 19:50
2

What this does is search for the identifying number preceeded by a string that has neither commas nor equal signs [in other words, their name] followed by and equal and replace all with nothing.

sed -r 's/[^,=]+=23456(,|$)//' new.txt
sam=12345,john=98765,bruce=67890,jack=54321,noah=67894,vision=65432,thor=98768,tony=89769,cap=90878
  • it is perfect.. Thank you tink.. Could you please explain this one – naren viru Dec 22 '18 at 19:52
  • @narenviru - adding comment to post. – tink Dec 22 '18 at 19:55
  • 1
    @tink: What if nick=23456 is at the end of the line? – Cyrus Dec 23 '18 at 6:51
  • Fair comment @Cyrus ... – tink Dec 23 '18 at 7:51

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.