Removing comma after 3rd column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing comma after 3rd column
# 8  
Old 03-14-2008
Quote:
Originally Posted by ShawnMilo
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.
you don't need a cat

UUOC
# 9  
Old 03-14-2008
Quote:
Originally Posted by matrixmadhan
you don't need a cat

UUOC
I know. I like cats. Leave them alone. Smilie

P.S.
Spay and neuter your pets (where applicable).
# 10  
Old 03-14-2008
I find your post very helpful!

bakunin, Thank you!
rubin, Thank you!
ghostdog74, Thank you!
ShawnMilo, Thank you!
matrixmadhan, Thank you!

Now I can drink my beer. Smilie
# 11  
Old 03-14-2008
Buddy,

You're very welcome! I'm glad we helped out. Enjoy your Friday night beer.

Shawn
# 12  
Old 03-15-2008
Hi, ShawnMilo.

When I run:
Code:
% cat data1 | perl -pe 's/(([^,]+,){2}[^,]+),/$1/'

for data1:
Code:
aaaa,bbbbb,cccccc,ddddddd
aaaaa,bbbbb,cccccc
aaaaa,bbbbb,cccccc,ddddddd,eeeeee
aaaaa,bbbbb,cccccc,ddddddd,eeeeee,fffffff

I get:
Code:
aaaa,bbbbb,ccccccddddddd
aaaaa,bbbbb,cccccc
aaaaa,bbbbb,ccccccddddddd,eeeeee
aaaaa,bbbbb,ccccccddddddd,eeeeee,ffffff

Which indeed omits the 3rd comma, but appears to leave all the commas after that. I thought the desired output was supposed to be:
Quote:
Originally Posted by buddyme
Code:
Output
aaaaa,bbbbb,ccccccddddddd
aaaaa,bbbbb,cccccc
aaaaa,bbbbb,ccccccdddddddeeeeee
aaaaa,bbbbb,ccccccdddddddeeeeeefffffff............(commas removed and joined)


What am I over-looking here? ... cheers, drl
# 13  
Old 03-15-2008
Another approach:

Code:
awk 'BEGIN{FS=","}{$1=$1",";$2=$2",";OFS="";$1=$1}1' file

Regards
# 14  
Old 03-17-2008
New solution, thanks to my friend Dave.

Code:
cat temp.txt | perl -ne 'chomp; @a = split(/,/,$_); printf("%s,%s,%s\n", $a[0],$a[1],join("",@a[2..$#a]))  '

I didn't realize at first that you wanted all of the commas after the second one stripped out.

If you have fewer than three commas to start with, you will get extra commas at the end. If you need three fields, this is good. If not, you can easily figure out how to strip trailing commas. (s/,+$//g)
 
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