Adding comma to end of each line if more than 1 line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding comma to end of each line if more than 1 line
# 1  
Old 02-13-2014
Adding comma to end of each line if more than 1 line

I have a file with dates as
Code:
'2013-01-01'
'2013-01-02'

I want the output to be
Code:
'2013-01-01','2013-01-02'

if there is only 1 entry then there should not be any comma.

Last edited by bartus11; 02-13-2014 at 03:17 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 02-13-2014
Try:
Code:
perl -0ne '$,=",";print split /\n/;print "\n"' file

# 3  
Old 02-13-2014
i cannot be using perl command
# 4  
Old 02-13-2014
Try :

Code:
$ awk '{printf "%s%s",NR==1 ? "" : OFS,$0}END{printf RS}' OFS=, file 
'2013-01-01','2013-01-02'

---------- Post updated at 02:24 AM ---------- Previous update was at 02:00 AM ----------

OR

Code:
$ tr -s '\n' ',' <file | sed 's/,$/\n/g'

# 5  
Old 02-13-2014
Code:
paste -sd, - <infile


Last edited by ctsgnb; 02-13-2014 at 04:15 PM..
# 6  
Old 02-13-2014
Quote:
Originally Posted by ctsgnb
Code:
paste -sd, - <infile

I think it could be made even shorter:
Code:
paste -sd, infile

# 7  
Old 02-14-2014
I think this is the simplest way for your problem.

cat Sample | tr "\n" ","
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

3. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

4. Shell Programming and Scripting

Adding semicolon at the end of each line

Hi, I have a script which I need to change. I want to add a semicolon at the end of each line where the line starts with "grant" for e.g. create table(.... ); grant select on TABL1 to USER1 grant select on TABL1 to USER2should become create table(.... ); grant select on TABL1 to... (3 Replies)
Discussion started by: pparthiv
3 Replies

5. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

6. Shell Programming and Scripting

Script adding ^M to end of line

I trying to make a simple script to get info from remote servers my problem is the output of this line- SERVER_NAME=`ssh -t $USER@$REMOTESERVER 'hostname'`the output is linux1^M I would like to remove the ^M where is my error? Many Thanks -Steve (1 Reply)
Discussion started by: shoodlum
1 Replies

7. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

8. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

9. Shell Programming and Scripting

Adding lines at end of a line

This is what I want to do. I want to write a script that reads each line (of the highlighted file below) and add a specific number of blank lines (sometime 2, 3 or 5 lines) at the end of each line while copying that line. For example, here is the input. The sky is blue. I like to eat. I like... (19 Replies)
Discussion started by: Ernst
19 Replies

10. Shell Programming and Scripting

Add a comma at end of every line

hello A small shell scripting help.. I have a file say with 5 lines of text (text file). At the end of everyline I need to add a comma at the end of the file. Thanks, ST2000 (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question