Make multiple lines into single quoted comma separated Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Make multiple lines into single quoted comma separated Linux
# 1  
Old 04-14-2014
Make multiple lines into single quoted comma separated Linux

Hi,

I want to change a file file1.txt:
Code:
1234
3456
2345
6789
3456
2333
4444

As, file2.txt in Linux:

Code:
'1234','3456','2345','6789','3456','2333','4444'

Could someone please help me. (Single liner sed, awk will be welcome!)

Last edited by Scrutinizer; 04-14-2014 at 05:21 AM..
# 2  
Old 04-14-2014
You can put the following sed script into one line, but i wouldn't do so to have it remain easy to read:

Code:
sed -n ":start
        $    {
               s/^/'/
               s/$/'/
               H
               g
               s/\n//gp
               q
             }
             s/^/'/
             s/$/',/
             H
             n
             b start" /path/to/file1 > /path/to/file2

I hope this helps.

bakunin
# 3  
Old 04-14-2014
Thanks for the reply.
This was my first post on this site, and I appreciate this quick response!
# 4  
Old 04-14-2014
Code:
awk -vq="'" '{print (q $0 q)}' ORS=',' file1.txt > file2.txt

# 5  
Old 04-14-2014
@SriniShoo: That leaves a trailing comma and no newline at the end..
# 6  
Old 04-14-2014
Thanks Scrutinizer for finding the issue with the code.
Here is the solution
Code:
awk -vq="'" 'NR == 1{print (q $0 q); next} {print ("," q $0 q)} END {print "\n"}' ORS='' file

# 7  
Old 04-14-2014
Some more:
Code:
awk '{printf q (NR>1?OFS:x) "%s", $1} END {print q}' q=\' OFS=",'" file

Code:
awk '{s=s (s?OFS:x) $1} END {print q s q}' q=\' OFS="','" file

Code:
awk '{$0=q $0 q; gsub(ORS,OFS)}1' q=\' RS= OFS="','" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comma separated values to individual lines

My OS : RHEL 6.7 I have a text file with comma separated values like below $ cat testString.txt 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', . . . . I want these values to appear like below 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', .... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Insert single quote on every word separated by comma

Hello, I have a text file as:-ABC BCD CDF DEF EFGI need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command anybody can help me out on this. Regards, Jas Please wrap all code, files, input & output/errors in CODE tags. It makes them easier to read and preserves... (12 Replies)
Discussion started by: jassi10781
12 Replies

3. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

4. Shell Programming and Scripting

Make Separated files from a single matrix - Perl

Hey Masters, Here is my input: fragmentID chromosome start end HEL25E TRIP1 r5GATC2L00037 chr2L 5301 6026 0.03 0.036 r5GATC2L00038 chr2L 6023 6882 -0.025 -0.041 r5GATC2L00040 chr2R 6921 7695 -0.031 0.005 r5GATC2L00042 chr2R 7715 8554 -0.006 -0.024 r5GATC2L00043 chr3L 8551 8798 0.042 0... (4 Replies)
Discussion started by: @man
4 Replies

5. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

6. Shell Programming and Scripting

Assigning Multiple Comma Separated IP's To A Bash Array

I am in the process of creating a BASH shell scripts for a project at work. So the scenario is as such: I have a file with each line entry separated by ':' ... (3 Replies)
Discussion started by: metallica1973
3 Replies

7. UNIX for Dummies Questions & Answers

sort comma separated lines by specific columns

Hello, I have a file which lines' words are comma separated: aa, bb, cc, uu b, ee, ff bb, cc, zz, ee, ss, kk oo, bb, hh, uu a, xx, ww tt, aa, dd, yy aa, gg I want to sort first by second column and in case of tie by fourth column with sort command. So the output would be: ... (4 Replies)
Discussion started by: asanchez
4 Replies

8. Homework & Coursework Questions

Find the files and make them comma separated files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi All, I am new to unix, my requirement is like need to find the files like DATA_FUNCTION* and put those... (1 Reply)
Discussion started by: madsongtel
1 Replies

9. Shell Programming and Scripting

Deleting a column in multiple files that are comma separated

Hi, I have a directory that contains say 100 files named sequencially like input_1.25_50_C1.txt input_1.25_50_C2.txt input_1.25_50_C3.txt input_1.25_50_C4.txt .. .. .. input_1.25_50_C100.txt an example of the content in each of the file is: "NAME" "MEM.SHIP" "cgd1_10" "cgd1_10"... (9 Replies)
Discussion started by: Lucky Ali
9 Replies

10. Shell Programming and Scripting

Separate lines in a single '|' separated line

Hi I have a file with contents like china india france japan italy germany . . . . etc.... I want the output as china|india|france|japan|italy|germany|.|.|. (3 Replies)
Discussion started by: hidnana
3 Replies
Login or Register to Ask a Question