SUMMARY: Script help

From: <Klas.Erlandsson_at_europolitan.se>
Date: Tue, 05 Jun 2001 13:30:54 +0200

Hi!

My question was:
"I have a big text file I want to edit.
Everywhere there is 11|XXXXX
I want to remove the last X (X could be any character) .
How do I solve this?"

I got many answers and many of them where the same and some had
misunderstood but
here I have the solution and with a well written explanation written by Mark
Deiss.
Thanks everybody!!!!

****************************************************************************
*****
"Usually would suggest ed command and perform an inline edit, but for this
you
may want to use sed and do a comparision of your final editted results
beforing
moving the editted version back over the top of the original.

$sed -e 's/\(11|....\)./\1/g' original_file > editted_file

Explanation of the regexp - the pattern \(11|....\) is holding any pattern
beginning with "11|" followed by any four characters into the first buffer.
So
the follow on "." is matching against the last character (5th X) that you
intend
to delete. Then in the second pattern space, only the content in the first
buffer is being brought back in; effectively you have now deleted the 5th
character . The initial "s" is signifying that you are performing a
substitution
operation and the trailing "g" signifys that you want to do this for every
occurrence found on any line. If you do not use the "g", then only the first
occurrence of your pattern on any line will be modified.

$diff original_file editted_file | more

Check that your edits are what you expected.

$mv editted_file original_file

If you want to replace your original with the editted version.

Regards Klas
Received on Tue Jun 05 2001 - 11:32:27 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:42 NZDT