Question on files - adding commas at the end


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question on files - adding commas at the end
# 1  
Old 09-26-2005
Question on files - adding commas at the end

Hi All,
I have a file. Each line suppose to contain 188 fields delimited by , (comma --> so 187 commas) but some records have less than 188 fields which needs to be filled by additional commas at the end.

Ex: a file wth 5 columns and 4 commas

test1,test2,test3
red1,red2
blue1,blue2,blue3,blue4
bad1
good1,good2,good3,good4,good5


Output should be:

test1,test2,test3,,
red1,red2,,,
blue1,blue2,blue3,blue4,
bad1,,,,
good1,good2,good3,good4,good5

Any help is appreciated.

Thanks,
Jingi
# 2  
Old 09-27-2005
MySQL

hi jingi,

here is the solution:

awk -F"," '
{
str=""
for (i=NF;i<=5;i++)
{
str=sprintf("%s%s",str,",")
}
printf("%s%s\n",$0,str)
}
' filename

Note: Change the 5 in for loop to number of "," you want in file at end.

regards,
rishi
# 3  
Old 09-27-2005
Try...
Code:
awk 'BEGIN{FS=OFS=","}{for(x=1;x<=n;x++)printf $x (x==n?ORS:OFS)}' n=188 file1 > file2

# 4  
Old 09-27-2005
try this

Code:
DEFAULT=5
j=1
for i in `awk -F"," '{print NF}' <filename>`
do
  final=`awk -F"," '{if( NR == '$j' ) print $0}' <filename>`
  while [ $i -lt $DEFAULT ]
  do
     final=$final","
     i=$(($i + 1))
  done
  echo "$final"
  j=$(($j + 1))
done

# 5  
Old 09-27-2005
Hi all,
I am getting the following error:

awk: record `SMAU,9628,SYDBUL083F...' has too many fields Smilie

Please help. Try the above scripts with 187 commas (required)

Thanks
# 6  
Old 09-27-2005
Quote:
Originally Posted by jingi1234
Try the above scripts with 187 commas (required)
Works fine for me. *shrug*
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a comma at the end

Hallo Team, I have a big file and the sample looks like below: 0105550 0105550 0105550 0105550 0105550 0125550 0125550 0125550 0125550 0125550 0215650 0215650 0215650 0215650 0215650 0315550 (3 Replies)
Discussion started by: kekanap
3 Replies

2. UNIX for Advanced & Expert Users

Help in adding a string at the end of each line and append files vertically

hi, i need a help in the script , need to append a string at the end of each line of a files , and append the files into a single file vertically. eg file1 has the following columns abc,def,aaa aaa,aa,aaa files 2 has the following rows and columns abc,def,aaa aaa,aa,aaa i... (3 Replies)
Discussion started by: senkerth
3 Replies

3. Shell Programming and Scripting

Adding info to end of line if two columns match from files with different separators

I have two files (csv and vcf) which look exactly like this S1.csv func,gene,start,info "exonic","AL","2309","het" "exonic","NEF","6912","hom"S1.vcf ##fileinfo #CHROM POS ID INFO chr1 4567 rs323211 1/1:84,104,99 chr4 2309 rs346742 1/1:27,213,90 chr6 5834 ... (5 Replies)
Discussion started by: Sarah_19
5 Replies

4. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

5. 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

6. Shell Programming and Scripting

Bash: adding commas in for loop

Dear Experts I think this is possibly the easiest thing. but I am not able to solve: I need comma to be added to end of each line echo'd. But does not want it to be added to last line. I have a script which does some data analysis and creates a command as in below code snippet for... (4 Replies)
Discussion started by: chakrapani
4 Replies

7. Shell Programming and Scripting

append file by adding commas

Hi, Iam working on Sun solaris machine.I have a file with ids in the below format. 1 2 3 4 5 I want it as: 1,2,3,4,5 I use these commands individually to get into that format: vi file /1,$s/$/,/g This adds commas in the file and then i save it. next i use this command :... (8 Replies)
Discussion started by: jyothi_wipro
8 Replies

8. UNIX for Dummies Questions & Answers

Inserting commas and replacing backslashes with commas

Hi, Newbie here. I have a file that consists of data that I want to convert to a csv file. For example: Jul 20 2008 1111 / visit home / BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105 Jul 21 2008 22222 / add friend / BlackBerry8830/4.2.2 Profile/MIDP-2.0... (3 Replies)
Discussion started by: kangaroo
3 Replies

9. Programming

Removing empty spaces and adding commas

I have a file which contains numbers as follows: 1234 9876 6789 5677 3452 9087 4562 1367 2678 7891 I need to remove the empty spaces and add commas between the numbers like: 1234,9876,6789,5677,3452, 9087,4562,1367,2678,7891 Can anyone tell me the command to do... (4 Replies)
Discussion started by: jazz
4 Replies

10. UNIX for Dummies Questions & Answers

adding a column at the end of the record

hello., i have a .txt file. inside the .txt file i have., /home/ss/cca.costco.transaction_date /home/sk/cca.costco.transaction_date /home/st/cca.costco.transaction_date /home/sv/cca.costco.transaction_date cca.costco.transaction_date is the file name. inside the file there are some... (2 Replies)
Discussion started by: pavan_test
2 Replies
Login or Register to Ask a Question