Cutting commas after the second occurrence in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting commas after the second occurrence in a line
# 1  
Old 12-19-2013
Question [Solved] Cutting commas after the second occurrence in a line

Hello everyone,

I am manipulating a large CSV file and am trying to read it into a program and started running into trouble. The have manually edited the file trying to make it correctly run through the program and have made progress. However, I am know stuck with an issue involving too many commas in lines. The correct number of commas in each line should be two but some lines have more than that. Here is an example of a clean line:

Code:
4343434,MyName,I went to a doctor

A messy line might look like this:

Code:
3232323,SomeName,I bought a car for 9007,and,9222

Notice the commas at the front are correct. So is there any way to remove any commas that appear in a line after the second occurrence? I was thinking about using awk or sed for this task but am not entirely sure how to go about this? Any help to resolve this problem would be greatly appreciated!

Last edited by tastybrownies; 12-19-2013 at 03:15 PM.. Reason: Edit
# 2  
Old 12-19-2013
Code:
echo "3232323,SomeName,I bought a car for 9007,and,9222" | sed ':begin;s/,/ /3;t begin'
3232323,SomeName,I bought a car for 9007 and 9222

# 3  
Old 12-19-2013
Try:
Code:
perl -a -F"," -ple '$_="$F[0],$F[1]," . join " ",@F[2..$#F]' file

# 4  
Old 12-19-2013
Actually this thread can now be disregarded. I just found a solution to my problem using this regular expression and notepad++. I went and reverted my previous edits and started fresh with the file.

Code:
^([0-9]{4,9}+\s)([#_0-9A-Za-z]+(\s))

I then replace with:

$1,$2,

What I was trying to do is solved.

---------- Post updated at 02:44 PM ---------- Previous update was at 02:43 PM ----------

I also thank you guys for your replies. They are educational for me and now I know a little bit more about scripting. I certainly appreciate it.

Moderator's Comments:
Mod Comment edit by bakunin: you are welcome. Changed your threads title accordingly.

Last edited by bakunin; 12-19-2013 at 04:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting all xml tags out of a line

I would like search and find a word (easily identified by 'key') from an xml file and then cut all of the tags out of the resulting line (anything between a < and a >) and display the remaining material. I am running Debian and mksh shell. dictionary.sh: #!/bin/sh key='key="'$1'"><form'... (3 Replies)
Discussion started by: bedtime
3 Replies

2. Shell Programming and Scripting

Assiging to a variable after cutting from the input line

Hi all, I am reading from the file having entries like below 111.ABC.POT 6477.YHT.OIT Now I need to read each line and cut each line seperated by dot and print into the file . I tried below and it is not working . Please help while read READLINE do eval... (4 Replies)
Discussion started by: Hypesslearner
4 Replies

3. Shell Programming and Scripting

Checking number of commas in each line.

Hi All, I am checking whether each line is having "n" number of commas or nor. In case not then I need to exit the process. I tried cat "$TEMP_FILE" | while read LINE do processing_line=`expr $processing_line + 1` no_of_delimiters=`echo "$LINE" | awk -F ',' '{ print NF }'` if ... (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

4. Shell Programming and Scripting

Cutting a part of line till delimiter

here are the few scenarios... isoSizeKB text NOT NULL, reserved1 varchar(255), KEY `deviceId` (`deviceId`) `d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci, `ePR` int(11) DEFAULT '0', PRIMARY KEY (`id`) ... (7 Replies)
Discussion started by: vivek d r
7 Replies

5. Shell Programming and Scripting

Cutting specific line of a file by checking condition

testfile.csv 0","1125209",,"689202CBx18888",,"49",,,"NONMC",,,,,"01112010",,,,,,,"MTM- "1","",,"689202ABx19005",,"49",,,"NONMC",,,,,"01072010",,,,,,,"MTM- testfile.csv looks like above format if the second column is null then get 23rd column and store in a different varible .. add all the... (1 Reply)
Discussion started by: mgant
1 Replies

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

7. Shell Programming and Scripting

Cutting columns starting at the end of each line...

Hi Guys, Can you help me with a sed or a csh script that will have an output from the input below. Cutting the columns starting from the end of the line and not from the start of the line? Sample1 - The underscore character "_" is actually a space...i need to put it as underscore here coz... (2 Replies)
Discussion started by: elmer1503
2 Replies

8. Shell Programming and Scripting

Third line occurrence replacement

You guys are really smart, so I'm hoping someone can help me out with this. I would like to match the third line occurrence of a pattern at the beginning of a line in a file and replace it. I'm ok at using sed but I have no clue about this one. Thanks in advance. (5 Replies)
Discussion started by: sirokket16
5 Replies

9. UNIX for Dummies Questions & Answers

Cutting a portion of a line seperated by pipe delimiter

Hi, In the below line a|b|10065353|tefe|rhraqs|135364|5347575 dgd|rg|4333|fhra|grhrt|46423|urdsgd Here i want to cut the characters in between the second and third pipe delimiter and then between fifth and sixth delimiter and retain the rest of the line. My output should be ... (3 Replies)
Discussion started by: ragavhere
3 Replies
Login or Register to Ask a Question