1

This is an example file of what I have

1321|4
512|2
1056|2
314|16
69|1
239|2
0|0
0|0
0|0
0|0
534|0

But I need the file to be in a format like this:

1321|4|512|2|1056|2|314|16|69|1|239|2|0|0|0|0|0|0|0|0|534|0

I tried my luck with awk but I'm not getting an usable result, never used it before and can't get examples and manuals into working code. How can I achieve the needed format? Any Help would be awesome.

  • This works more or less, if I open it in Notepad on Windows I have the wished format, but in every other editor it's the same as before just with an extra | in front of the values. If i echo it to the terminal or use cat or so I get |0|0|22 as output –  Aug 08 '14 at 14:03

4 Answers4

1

You can use this

sed ':a ; N ;s/\n/|/g ; t a '

Explanation

 t loop  -- Loop continued until the substitution false or EOF occur .
 N       -- Get the two lines and stored in the pattern space .

N get the two lines and stored in the pattern space so the pattern space having two lines like line1\nline2 then substitution perform like \n to | like line1|line2 ,so the pattern space having one line and loop continue then get another one line and substitution perform. Once EOF founded the loop terminated and print the pattern space .

Kalanidhi
  • 4,902
  • 27
  • 42
  • I think it would be more helpful for the OP and further visitors, when you add some explaination to your intension. – Reporter Aug 08 '14 at 10:49
  • @reporter,I had updated my answer. – Kalanidhi Aug 08 '14 at 11:15
  • Thanks, this works more or less, if I open it in Notepad on Windows I have the wished format, but in every other editor it's the same as before just with an extra | in front of the values. If i echo it to the terminal or use cat or so I get |0|0|22 as output –  Aug 08 '14 at 14:04
1

This should do:

tr '\n' '|' < file; echo

Or this:

awk -v ORS=\| '$1=$1 END {print RS}' file
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • 1
    `tr '\n' '|' < file` suffices. Also, this is answered exactly the same in the duplicate. Also, both solutions have the problem of missing a new line at the end. – fedorqui Aug 08 '14 at 10:17
  • @fedorqui Thanks. Newline can be fixed on `awk` like this `awk -v ORS=\| '$1=$1 END {print RS}' file`, but it still give an extra unwanted `|` – Jotne Aug 08 '14 at 10:24
  • The tr version contains an extra vertical bar at the end --- don't know if that's an issue. – jas Aug 08 '14 at 10:25
  • newline can be fixed by just echoing after. `tr '\n' '|' < file; echo` –  Aug 08 '14 at 10:33
  • @Jidder Thanks for info. It still does not fix the extra `|`, so my vote went to Jas :) – Jotne Aug 08 '14 at 10:37
  • @Jotne could do this `tr '\n' '|' < file ;echo -e "\b "` –  Aug 08 '14 at 10:56
  • @Jidder It still prints one `|` unwanted at the end of the line. It could off course be removed by another command, but its nicer and faster to get all in one. – Jotne Aug 08 '14 at 11:17
  • `"\b "` removes the last `|`. Did you leave the space after `\b` in the command ? –  Aug 08 '14 at 11:42
  • @jidder my fault, did have a blank line in my test file. ufff – Jotne Aug 08 '14 at 11:56
1

I'm not sure awk is the best hammer for this nail, but

awk '{ printf("%s%s", sep, $0); sep = "|" }' sample.txt

should do it.

And here Jotne's improved version from the comments, adding a final newline:

awk '{ printf("%s%s", sep, $0); sep = "|" } END { print "" }' sample.txt
jas
  • 10,715
  • 2
  • 30
  • 41
  • 1
    Adding an `END` section and it would be perfect. `awk '{printf("%s%s",sep,$0);sep="|"} END {print ""}'` – Jotne Aug 08 '14 at 10:26
  • Shorter but not different enough for my own answer `awk '{ printf(NR>1?"|"$0:$0)}' test;echo` also removes the first bar that shouldn't be there. –  Aug 08 '14 at 11:12
0

Shell only (though it does require one sub-shell:

read -d '' -r -a lines < file
(IFS=\|; echo "${lines[*]}")
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148