I have a file containing strings delimited by any of the following undesired characters:
<
>
|
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?
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 tr
ansliterate 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 \n
ewline delimiters and using tools designed to handle \n
ewline 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:
s
command primitive followed by a delimiter (typically /
)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 tr
ansliteration function.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments