Removing comma after 3rd column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing comma after 3rd column
# 1  
Old 03-13-2008
Removing comma after 3rd column

I have 10,000 lines to remove the commas after the 3rd column. Any help will be appreciated!

Removing commas after the 3rd column

Input
aaaaa,bbbbb,cccccc,ddddddd
aaaaa,bbbbb,cccccc
aaaaa,bbbbb,cccccc,ddddddd,eeeeee
aaaaa,bbbbb,cccccc,ddddddd,eeeeee,fffffff........(limit of comma is not known)


Output
aaaaa,bbbbb,ccccccddddddd
aaaaa,bbbbb,cccccc
aaaaa,bbbbb,ccccccdddddddeeeeee
aaaaa,bbbbb,ccccccdddddddeeeeeefffffff............(commas removed and joined)
# 2  
Old 03-13-2008
This works but it removes the comma of the 3rd column only.
sed 's/,//3' myfile1

aaaaa,bbbbb,ccccccddddddd
aaaaa,bbbbb,cccccc
aaaaa,bbbbb,ccccccddddddd,eeeeee
aaaaa,bbbbb,ccccccddddddd,eeeeee,fffffff

I want something like this but it does not work, sed 's/,//3~...' myfile1
# 3  
Old 03-14-2008
As it isn't possible to match "the third and any following character" directly you could use a loop: You search for lines with at least 3 commata, then remove the third one only. Then you re-search the same line again.... This way you remove the third commata one by one until you have removed them all:

Code:
sed -n ':start
/^[^,]*,[^,]*,[^,]*,/ {
     s/^\([^,]*,[^,]*,[^,]*\),/\1/
     b start
     }
p'

I hope this helps.

bakunin
# 4  
Old 03-14-2008
Or:

Code:
awk 'BEGIN {FS=","; OFS=","}
  NF >3{x=$3"," ; y=match($0,x);  string=substr($0,y);  gsub(/\,/,"",string); print $1, $2, string}
  NF<=3{print $1,$2,$3}' file

# 5  
Old 03-14-2008
Code:
awk 'BEGIN{FS=","}
{  for(i=3;i<=NF;i++) {   s=s $i  }
   printf "%s,%s,%s\n", $1,$2,s
   s="" 
}' "file"

# 6  
Old 03-14-2008
Perl One-Liner

Code:
cat temp.txt  | perl -pe 's/(([^,]+,){2}[^,]+),/$1/'


Explanation:

Find two of these: one or more non-commas followed by a comma

After that, find one or more non-commas

After that, find a comma.

Replace all that with just the first two parts.
# 7  
Old 03-14-2008
and this,

Code:
awk -F"," '{ printf "%s,%s,", $1, $2; for( i=3; i<= NF; i++ ) { printf "%s", $i } printf "\n" }' filename

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
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

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

4. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

5. Shell Programming and Scripting

How to use regex on particular column (Removing comma from particular column)?

Hi, I have pipe separated file which contains some data having comma(,) in it. I want to remove the comma(,) only from particular column without changing data in other columns. Below is the sample data file, I want to remove the comma(,) only from 5th column. $ cat file1 ABC | DEF, HIJ|... (6 Replies)
Discussion started by: Prathmesh
6 Replies

6. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!102.56.1.101!30!50!/ other host server 2 des check_ssh_disk!192.6.1.10!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

7. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

8. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

9. Shell Programming and Scripting

Removing comma just from numbers

Hi Guys, I want to remove commas from just the numbers in the file. So both sides of the comma should be numbers. Input file Johan 1,234 nb jan 123,3 hi, hello, bye 12,345,54 hejhej Desired output: Johan 1234 nb jan (6 Replies)
Discussion started by: Johanni
6 Replies

10. Shell Programming and Scripting

rearrange the column names with comma as column delimiter

Hi, I am new to shell scripting, i have requirement can any one help me out in this regrads, in directory i have file like invoice1.txt, invoice2.txt in each file i have fixed number of columns, 62 in number but they are randomly arranged.like for first file invoice1.txt can have columns... (5 Replies)
Discussion started by: madhav62
5 Replies
Login or Register to Ask a Question