Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

A text file has contents something like

chair
table
pen
desk

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

('chair','chair'),('table','table'),('pen','pen'),('desk','desk')

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

5 Answers 5

up vote 3 down vote accepted

One way using sed:

Content of script.sed:

## Change line.
s/.*/('&','&')/

## Append it to hold space.
H

## In end of file substitute newlines with commas and print.
$ {
    g   
    s/^\n//
    s/\n/,/g
    p   
}

Command:

sed -nf script.sed infile

Output:

('chair','chair'),('table','table'),('pen','pen'),('desk','desk')
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)
var=${var:1}

Update:

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.

var=
while IFS= read -r line; do
  var="$var,('$line','$line')"
done <input_file
var=${var#,}

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" ',')
var=${var%,}
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

 
discard

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.