Help on sed requested

Hi

I have a problem to resolve, I think sed is the best option, and I am not successful yet.

Have a UNIX file which has records as of the 2 character state codes like
NY
NJ
PA
DE

From the file I need to create this as a variable in the same script or another file - ('NY','NJ','PA',DE')

The file can contain more state codes.

The output need to be passed on to a sequel in the where clause -
select * from taxcode
where state_code in ('NY','NJ','PA',DE')

So far I am successful only in adding a comma to all the 4 lines of records.

Please help.

sed "s/.*/'&'/" file | tr '\n' ',' | sed "s/,$//"

Thanks anbu23 for the quick help.
i tried, but it did not work, i tried to write the output to a file like this

sed "s/.*/'&'/" file | tr '\n' ',' | sed "s/,$//" > myfile

myfile was created with 0 byte.

how will you get the output to a variable?

thanks again for the help.

hi,
Anbu code works as expected and redirection also works fine.

To get output in variable:

a=`sed "s/.*/'&'/" 1_ip | tr '\n' ',' | sed "s/,$//"`
echo $a
$ cat cod
NY
NJ
PA
DE

$ var=$(echo $(<cod) | sed "s:^ *:(\':;s: *$:\'):;s: :\',\':g")
$ echo $var
('NY','NJ','PA','DE')
$ echo "select * from taxcode where state_code in $var"
select * from taxcode where state_code in ('NY','NJ','PA','DE')
$

try to apply Anbu's code step by step so that where you get error maybe you missing something in syntax?

sed "s/.*/'&'/" file | tr '\n' ',' | sed "s/,$//"
  1. Did first part of command added " ' " around each fields like 'PA'?
  2. Did second part changed newlines to commas?
  3. the commas at the end of each line were removed?

Thanks everyone, Thanks ctsgnb.
It worked.

Making no empty promises, but I will try my best to come back with - the function of each symbols in the command.

---------- Post updated at 05:20 PM ---------- Previous update was at 05:19 PM ----------

sorry, missed to add this -
the function of each symbols in the command - which ctsgnb used

Hi,

Another 'sed' solution:

sed -n "H ; $ {x;s/^\n/('/;s/\n/','/g;s/$/')/;p}" infile

Regards,
Birei