2

I need to remove part of a line in a group of files.

Example line:

TRN*1*301444/05-13-20*6549873211~

I need it to be this instead (remove the portion /05-13-20):

TRN*1*301444*6549873211~

The forward slash up to the last asterisk need to be removed and the new line needs to be retained in the file.

2

2 Answers 2

3

With sed:

sed -n 's_/[^*]*__p'
  • /[^*]* matches the portion from / uto next *, and it is then replaced with null, as we have used empty string in the replacement

Example:

% sed -n 's_/[^*]*__p' <<<'TRN*1*301444/05-13-20*6549873211~'
TRN*1*301444*6549873211~
0

Regular expressions can be used to do that. They are available in most languages, but perl is the regex leader. There are many ways to do what you asked with regex but here is one example;

perl -pi -e 's/\/.*\*/*/g' file.txt

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.