0

I have a text file like this:

X0122
Yellow Car
$1200
YK321
Black Car
$400
BZ321132
Red Boat
$2344

every line is separated by a \n

I want to join every 3 elements in a line separated by ;. So, the file will be converted into this:

X0122;Yellow Car;$1200
YK321;Black Car;$400
BZ321132;Red Boat;$2344

How do I do that on unix?

Note: the files may contain accented letters like á, ó, ã and single quotes and are coded in "UTF-8 Unicode English text, with very long lines", like the command file say about them.

2

You can use paste:

$ paste -sd';;\n' file
X0122;Yellow Car;$1200
YK321;Black Car;$400
BZ321132;Red Boat;$2344
2

With awk:

awk 'ORS=NR%3?";":RS'

Output:

X0122;Yellow Car;$1200
YK321;Black Car;$400
BZ321132;Red Boat;$2344

Explanation: Depending on the line number, the output record separator is set either to a semicolon or to a newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.