Unix bash Sed command Combination on one file

Aman

I have a file containing strings delimited by any of the following undesired characters:

  1. <
  2. >
  3. |
  4. <space>

It looks like this:

...  > Test Item|Test Item ...

The above ... ellipses represent continuation. The file is comprised of only a single line.

I would like to modify the file so that the strings are delimited instead with only a single <space> char. For example:

... Test Item Test Item ...

With sed I tried:

sed -e 's/< >*| */|//g' filename

But that returned the following error message:

sed: 0602-404 Function s/<  *| */|//g cannot be parsed.

What to do?

mikeserv

If there is no chance that any of the characters <> | might occur within the data you wish to retain in your file, then almost definitely the most efficient solution is simply to transliterate them away entirely:

tr '<>|' '   ' <infile |    ###translate all delimiter chars to spaces
tr -s ' '     >outfile      ###pipe results to second tr and squeeze spaces

sed may work for you as well, but if it does, it will almost definitely be slower in doing so. You should be careful when working w/ input files not based around \newline delimiters and using tools designed to handle \newline delimited data (such as sed and grep) because some implementations may collapse under buffer strain.

Your issue is not (yet) that, though: rather you have submitted invalid syntax as a sed command. @Archemar has already commented on what it is, but here is why:

A sed s///ubstitution is a command composed of three fields:

  1. the s command primitive followed by a delimiter (typically /)
  2. the regular expression pattern field followed by the same delimiter
  3. the replacement field followed by the delimiter and optional flags

Either or both of the latter two may be zero-length for different reasons - and so s/// is (context-depending) acceptable syntax. This means that the sed parser must depend very heavily on the delimiter. In your command you specify too many fields - which is a syntax error, and is why your function cannot be parsed.

If your implementation's sed can handle the single-line input and is POSIX-compatible, then this should work instead:

sed 'y/<>|/   /;s/  */ /g' <infile >outfile

...where the y primitive above is sed's own transliteration function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related