Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A text file has contents something like


Now i want it to be changed and stored in a variable say var as below


is it possible?

EDIT Jofel's anser gave following error

$ sed ':a;N;$!ba;s/\n/,/g;s/\w*/(''&'',''&'')/g' -i csclm.txt
sed: The label :a;N;$!ba;s/\n/,/g;s/\w*/(&,&)/g is greater than eight characters.

I am using :

$ uname -a
HP-UX rcihp145 B.11.23 U 9000/800 3683851961 unlimited-user license
share|improve this question
up vote 3 down vote accepted

One way using sed:

Content of script.sed:

## Change line.

## Append it to hold space.

## In end of file substitute newlines with commas and print.
$ {


sed -nf script.sed infile


share|improve this answer
working thanks @Birei – munish Feb 23 '12 at 12:34

No need for a subprocess, it can be done in pure bash shell:

var=$(while read line; do echo -n ",('$line','$line')"; done < file)


If you want this as a one-liner, you could:

var=$({ read line && echo -n "('$line','$line')" && while read line; do echo -n ",('$line','$line')"; done } < file)

Note the && to perform echo and whileonly it the file is non-empty.

share|improve this answer
++++1 this looks nice and short – munish Feb 23 '12 at 12:44
Hi @jfgagne but the first comma will come and the output will be start something like ,(......... – munish Feb 23 '12 at 13:22
Yes, the 1st comma will will come from the 1st command. This is why you need to add var=${var:1} as a 2nd command to stripe the 1st character. I think it is easier to proceed that way than to manage the 1st line in a different way, but I will update the post. – jfgagne Feb 23 '12 at 18:55

Here's a pure shell method: read every line, and append the properly massaged data to var. Strip off the extra comma.

while IFS= read -r line; do
done <input_file

Here's a simple method using external utilities: massage each line with sed, then turn the newlines into commas. Strip off the extra comma.

var=$(<input_file sed "s/.*/('\1','\1')/" | tr "\n" ',')
share|improve this answer

Here a solution not with awk, but with sed:

To do the replacement in FILENAME, run

sed ':a;N;$!ba;s/\n/,/g;s/\w*/(''&'',''&'')/g' -i FILENAME

For more information, see this question.

share|improve this answer
thanks for the sugestion, but it did not run.it gave – munish Feb 23 '12 at 10:48
$ sed ':a;N;$!ba;s/\n/,/g;s/\w*/(''&'',''&'')/g' -i csclm.txt sed: The label :a;N;$!ba;s/\n/,/g;s/\w*/(&,&)/g is greater than eight characters. – munish Feb 23 '12 at 10:48
+1 i like your answer anyway i am going to look into it.Its a step in the right direction – munish Feb 23 '12 at 11:00
For me it runs without any problems. I tried it with GNU sed 4.2.1 and zsh/bash as shell. – jofel Feb 23 '12 at 11:32
You seems to use non-GNU sed. According to this discussion, it should work for you if you use real newlines instead of ; between the sed commands. – jofel Feb 23 '12 at 11:40

A very complicated way to do it, working only in the bash shell (because of process substitution), but using only simple commands:

var=$(sed -e "s/^/'/" -e "s/\$/'/" file |
  paste -d "," - <(sed -e "s/^/'/" -e "s/\$/'/" file) |
  sed -e "s/^/(/" -e "s/\$/)/" |
  paste -s -d ",")
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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