I have following values in a file in separate lines:
California
New York
Washington
South Carolina
Kansas
What would be unix script to display them in a single line (as showb below)
'California', 'New York', 'Washington', 'South Carolina', 'Kansas'
[I do not want to have any intermediate file to achieve this.. just an echo code is fine]
Use this command
tr '\n' ',' < input_file
For single quotes use
sed -e "s/^/'/" input_file | sed -e "s/$/'/" | tr '\n' ','
(Not tested for single/double quote escaping issues)
For variable
NEW_VAR=$(echo $VAR | sed -e "s/^/'/" | sed -e "s/$/'/" | tr '\n' ',')
You need to use awk to format the output. Does
sed and awk
ring a bell?$
grep input file | awk '{print}' ORS=', '
Then concatenate your string and look out for the proper output.
with awk you can try this
You can also try this