Reputation: 13
I am very new to the bash programming and need to convert a single text column to a single row and then separate the characters in the row based on the pattern.
I have text document with the column, which has one letter with six digits in each line:
a111111
b222222
c333333
d444444
e555555
I need to transform the column above into the following row:
'a111111','b222222','c333333','d444444','e555555'
Could someone please advise how this can be achieved?
Upvotes: 1
Views: 42
Reputation: 785068
You can use awk
with printf
:
awk -v ORS=, 'NR>1{printf "%s", ORS} {printf "\x27%s\x27", $0}' file
\x27
prints a single quote.
For the 2nd record onwards it will prints ORS
(which is set to comma) at start and then the quoted line will be printed.
Output:
'a111111','b222222','c333333','d444444','e555555'
Upvotes: 2
Reputation: 246774
Another approach:
sed -r 's/^|$/\x27/g' file | paste -sd,
sed adds the single quotes at the beginning and end of each line, and paste joins the line together with commas
Or, print a comma for each line, and when you're done back up 1 character and overwrite the last comma with a space:
awk '{printf "'\''%s'\'',", $0} END {printf "\b \n"}' file
Upvotes: 1