Home > Software design >  Trimming the end of words in a txt file
Trimming the end of words in a txt file

Time:06-18

I have a text file in the form of a binary matrix. The headers of each column are in the format:

"ABCD-1S2S-123-01A" "ABCD-2D4F-245-01A" "ABCD-2K4K-013-01A" etc

Is there a way to remove the -01A from all incidences in the file? I'm using Linux so I have tried to use sed:

sed 's/-01A//g' text_name.txt

CodePudding user response:

What you are doing, is completely correct, but there's a difference between 01A (starting with the digit zero) and O1A (starting with the letter 'O').

This is the result I'm getting:

sed 's/-01A//g' text_name.txt
"ABCD-1S2S-123" "ABCD-2D4F-245-O1A" "ABCD-2K4K-013" etc

As you see, the -01A has disappeared from the first and the third entry, the second entry has not modified as there was a letter instead of a digit.

You might need to realise that the result is shown on screen. In case you want it to do the replacement on the file itself, you need to redirect sed output towards an outputfile, something like:

sed 's/-01A//g' text_name.txt >result_text_name.txt

The result* file contains the required results.

  • Related