Insert comma based on max number of column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert comma based on max number of column
# 1  
Old 12-10-2008
Insert comma based on max number of column

Hi,

I am new to unix shell shell scripting. I have a specific requirement where I need to append comma's based on the max number of column in the file.

Eg:

If my source file look something like this,

sengwa,china
tom,america,northamerica
smith,america
walter

My output file should should look like
sengwa,china,
tom,america,northamerica
smith,america,
walter,,

Please help me to get this resolved.

Regards,
Sengwa
# 2  
Old 12-10-2008
try this!!
Code:
awk -F"," 'NF<3{print $1","$2","}NF>2{print $0}' filename

# 3  
Old 12-10-2008
Hi,

Thanks for your reply. The script you provided is working for only maximum of 3 columns. But the number of column will vary in the source file.

Best regards,
Sengwa
# 4  
Old 12-10-2008
Quote:
Originally Posted by nicholas_ejn
Hi,

Thanks for your reply. The script you provided is working for only maximum of 3 columns. But the number of column will vary in the source file.

Best regards,
Sengwa
provide some more input data
# 5  
Old 12-10-2008
Hi Sengwa!

Interesting problem,

I cooked up something like this, it should work for even larger number of columns. It has to read through the file twice, although one could get away with saving it all in an array but since I don't know how many lines and columns there may be, it could be huge, I guess this will do:

Code:
lakris@ubuntu:~/projekt/scripts/maxcol$ cat file
sengwa,china
tom,america,northamerica
smith,america
walter

Code:
lakris@ubuntu:~/projekt/scripts/maxcol$ cat col.sh 
#!/bin/bash
maxval=0
while read line
do
newval=$(echo $line|tr , " "|wc -w)
[ $newval -gt $maxval ] && maxval=$newval
done < file
>newfile
while read line
do
arr=($(echo $line|tr , " "))
echo -n ${arr[0]} >> newfile
for ((i=1;i<$maxval;i++))
do
echo -n ",${arr[$i]}" >> newfile
done
echo >> newfile
unset arr
done < file

Code:
lakris@ubuntu:~/projekt/scripts/maxcol$ chmod +x col.sh 
lakris@ubuntu:~/projekt/scripts/maxcol$ ./col.sh

Code:
lakris@ubuntu:~/projekt/scripts/maxcol$ cat newfile 
sengwa,china,
tom,america,northamerica
smith,america,
walter,,

Will it work? I guess one could get away from using tr, echo and stuff and use some bash inline parameter modifications instead...
Smilie


/Lakris
# 6  
Old 12-10-2008
nawk -f nic.awk myFile myFile

nic.awk:
Code:
BEGIN {
  FS=OFS=","
}
NR==FNR{
  nf=(NF>nf) ? NF : nf
  next
}
{NF=nf; $1=$1;print}

# 7  
Old 12-11-2008
This much also works

Code:
 awk 'BEGIN {
  FS=OFS=","
}
NR==FNR{
  nf=(NF>nf) ? NF : nf
  next
}
{NF=nf; print}'

$1=$1 is confusing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Filter Row Based On Max Column Value After Group BY

Hello Team, Need your expertise on following: Here is the set of data: C1|4|C1SP1|A1|C1BP1|T1 C1|4|C1SP2|A1|C1BP2|T2 C2|3|C2SP1|A2|C2BP1|T2 C3|3|C3SP1|A3|C3BP1|T2 C2|2|C2SP2|A2|C2BP2|T1 I need to filter above date base on following two steps: 1. Group them by column 1 and 4 2.... (12 Replies)
Discussion started by: angshuman
12 Replies

4. Shell Programming and Scripting

Insert value of column based on file name matching

At the top of the XYZ file, I need to insert the ABC data value of column 2 only when ABC column 1 matches the prefix XYZ file name (not the ".txt"). Is there an awk solution for this? ABC Data 0101 0.54 0102 0.48 0103 1.63 XYZ File Name 0101.txt 0102.txt 0103.txt ... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

5. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

6. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

7. Shell Programming and Scripting

Insert comma in place of column

Hi all, I have a file in which I have to insert commna between entries of 2 column and createa new file separated by commas not a columns if input is FHIT Adenosine Monotungstate Not Available CS Trifluoroacetonyl Coenzyme A Not Available Theo expected output is ... (5 Replies)
Discussion started by: manigrover
5 Replies

8. Shell Programming and Scripting

Count number of column in a comma delimited file

I have a comma (,) delimited file. 106232145,"medicare","medicare,medicaid",789 I would like to count the number of fields in each line. I tried the below code awk -F ',' '{print NF-1}' This returns me the result as 5 instead of 4. This is because the awk takes... (9 Replies)
Discussion started by: machomaddy
9 Replies

9. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

10. Shell Programming and Scripting

Insert new line based on numerical number of column

My input file: Class Number Position Range 1 Initial 50 1 Initial 50 2 Terminal 150 2 Terminal 20 2 Single 10 3 Single 20 4 Double 50 5 Initial 50 5 Initial 60 Class Number... (11 Replies)
Discussion started by: patrick87
11 Replies
Login or Register to Ask a Question