Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List processing, convert list to apostrophe and comma separated records, surrounded by brackets

Tags:

bash

awk

I have a list in a file named Target_id_convert.txt

70S ribosome
ALK tyrosine kinase receptor
ATP
ATP synthase

Desired output

('70S ribosome','ALK tyrosine kinase receptor','ATP','ATP synthase')

I have written this code

sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/" Target_id_convert.txt  > Target_id_convert1.txt
tr '\n' ',' < Target_id_convert1.txt > Target_id_convert_output.txt

I then have to manually edit the file and add () in the Target_id_convert_output.txt file, Kindly let me know how to do it efficiently and all in one go, as It is all supposed to be automated.

like image 899
KHAN irfan Avatar asked Nov 22 '25 09:11

KHAN irfan


2 Answers

This awk one-liner should do what you want:

awk -v q="'" '{$0=q $0 q;printf "%s%s", (NR==1?"(":","),$0}END{print ")"}' file

I declared a var q to have single quote ('), to avoid many escaping.

like image 132
Kent Avatar answered Nov 23 '25 22:11

Kent


Assuming your records are double new-line separated, I would go with a sed/awk combo:

<file sed "/[^[:blank:]]/ s/.*/'&'/g" |
awk '{ $1=$1; print "(" $0 ")" }' RS= FS='\n' OFS=,

If the input is:

70S ribosome
ALK tyrosine kinase receptor
ATP
ATP synthase

70S ribosome
ALK tyrosine kinase receptor
ATP
ATP synthase

Output is:

('70S ribosome','ALK tyrosine kinase receptor','ATP','ATP synthase')
('70S ribosome','ALK tyrosine kinase receptor','ATP','ATP synthase')
like image 27
Thor Avatar answered Nov 23 '25 23:11

Thor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!