Frage

I want convert this text on a given file:

87665
S
3243423
S
334243
N
...

to something like this:

87665,S
3243423,S
334243,N
...

I've been reading some similar questions, but it didn't work... is there a way to do this with a single line command in linux? Thanks!

War es hilfreich?

Lösung

Using sed:

sed '$!N;s/\n/,/' filename

Using paste:

paste -d, - - < filename

paste would leave a trailing , in case the input has an odd number of lines.

Andere Tipps

Something like this might work for you:

$ awk 'NR%2{a=$0;next}{print a","$0}' file
87665,S
3243423,S
334243,N

To handle files with odd lines, you can do:

awk '{printf "%s%s", $0, NR%2?",":ORS}' file

Just for fun, a pure bash solution:

while IFS= read -r l1; do
    read -r l2
    printf '%s\n' "$l1${l2:+,$l2}"
done < file

If there's an odd number of lines, the last line will not have a trailing comma.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top