Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert line feeds to pipe char & reformat 1

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
0
0
GB
I have a file which consists of repeated field values on separate lines. I want to convert it so that the values are on the same line separated by a pipe char (& then a new line for each new record). e.g. my original file is

Code:
4
The Beatles
Liverpool
5
The Rolling Stones
London
3
The Who
London

and I want it to look like:

Code:
4|The Beatles|Liverpool
5|The Rolling Stones|London
3|The Who|London

The only way I can think of doing this is to use split (to split into a series of files 3 lines long); tr (to translate the end of line char to '|'; cat (to put the files back together. But I know this is a poor way of doing it. I'm guessing there's a way to do the same in awk, or maybe use sed somehow - but my skills don't go that far.

Can anyone suggest an efficient - & hopefully understandable/maintainable :) - way of doing it?

Thanks, Chris
 
That is great - thank you feherke.

I know I should learn tools like awk properly but I find the syntax baffling. Thank you
 
<b>Is there something to say start processing at line 2 – eg </b>

Header Record
4
The Beatles
Liverpool
5
The Rolling Stones
London
3
The Who
London

<b>And the result be </b>

Header Record
4|The Beatles|Liverpool
5|The Rolling Stones|London
3|The Who|London

Thanks in advance.
 
The file has following type of data and need to reformat it

"3P" "1" "" "28-AUG-1900"
"REV"
"5674023"
"97418"
"6946311"
""
"TRAINING SOLUTIONS LTD"
"REV"
"940000"
"97419"
"6907016"
""
"MR CN SIA GILL"

Need the file to formatted like the following

"3P" "1" "" "28-AUG-1900"
"REV"|"5674023"|"97418"|"6946311"|""|"TRAINING SOLUTIONS LTD"
"REV"|"940000"|"97419"|"6907016"|""|"MR CN SIA GILL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top