insert pipe char

E

eeb4u

I would like to insert a pipe char at the beginning of certain lines in
a data file. I would like this to occur whenever a line ends with a
pipe.

The file has many records, with normally a two lines per record
(sometimes three). The first line is a "header" and the second is the
text. The pipe is a delimiter.

below is a short data example:

field 1|field 2|field 3|
text text text text text
field 1|field 2|field 3|
text text text text text
field 1|field 2|field 3|
text text text text text

I need the pipe before field 1, another process will look for this pipe
to identify a header line.

text lines never contain pipe chars.

Using perl, can I accomplish this or should I use sed etc. I have been
using perl to get the data into this format, but I am open to
suggestions

thanks for any advice,

Mike Dundas
 
B

Bart Lateur

I would like to insert a pipe char at the beginning of certain lines in
a data file. I would like this to occur whenever a line ends with a
pipe.

s/^/|/ if /|$/

Put this in a 1-line command in your command line shell, and you can do
it without an actual script.

perl -pi~ 's/^/|/ if /|$/' FILE

or, on DOS:

perl -pi~ "s/^/|/ if /|$/" FILE


Drop the -i and whateverfollows if you don't want this to change the
file in place.

perl -p 's/^/|/ if /|$/' FILE >NEWFILE
 
B

Brad Baxter

Using perl, can I accomplish this or should I use sed etc. I have been
using perl to get the data into this format, but I am open to
suggestions

Just curious: If you're using perl to get the data into this format,
can't you insert what you need then?
 
E

eeb4u

Brad said:
Just curious: If you're using perl to get the data into this format,
can't you insert what you need then?
My perl is very limited (very). I get a file from one system in a
somewhat similar format but each record has header and data portion on
the same line. I am capable of splitting the two onto separate lines
with $_=~s/\|"/\|\n"/g; (header part it delimited with pipes), along
with stripping space runs, leading/trailing space and correcting
hyphenation etc. Unfortunately, I was unable to find examples on the
web that were similar enough to my problem where I could modify them
and stick them in my script. I will use the above suggestions, and
post again if I have any problems.

Thanks for all your help.

Mike D

P.S.
I wouldn't post for the fun of it! Why don't you add RTFM to all your
replies Mr. B. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top