Editing Commas in a textfile using sed

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing Commas in a textfile using sed
# 1  
Old 09-10-2009
Editing Commas in a textfile using sed

Hi guys
task removing the last commas of 5th and 6th columns. The bug in the script is causing effect because of whitespaces around commas. I tried to delete white spaces first and running the above script. but still some where getting the results wrong.

I already have a script to do this task but somewhere bug located in the script. Could please debug it. Thanx


sed -e 's/,[^0-9]//g' -e 's/,$//g' file_name

7829885 7831552 + 1    1667,               0,
35934936 35937087 - 2  1281,870,       0,1281,
35934936 35937087 - 2  1281, 870,      0 ,1281 ,
35934936 35937087 - 2  1281 , 870 ,    0, 1281 ,

output should be like this
7829885 7831552 + 1 1667 0
35934936 35937087 - 2 1281,870   0,1281
35934936 35937087 - 2 1281,870   0,1281
35934936 35937087 - 2 1281,870   0,1281

Last edited by repinementer; 09-10-2009 at 09:44 AM..
# 2  
Old 09-10-2009
The first line of expected output does not follow the same rules for output as do lines 2 .. 4
It has all commas removed. Please clarify.
# 3  
Old 09-10-2009
The first line of expected output does not follow the same rules for output as do lines 2 .. 4
It has all commas removed. Please clarify.

Need to remove the commas of last number like 870 in the 5th column or 1281 in the 6th column. other commas have to stay as it is.
# 4  
Old 09-10-2009
somewhat ugly:
sed 's/ *, */,/g;s/,$//;s/,/ /2;s/,/ /3;/+/s/,/ /1' myFile

# 5  
Old 09-10-2009
Thanx for effort even though its ugly
What if the it not just about 5th and 6th columns. If they happen to appear in 7th and 8th columns is the same code applies?

---------- Post updated at 05:56 AM ---------- Previous update was at 05:53 AM ----------

I think its working perfect. If you explain the code that really helpful to me and others so that we could play with the script for other requirements.
# 6  
Old 09-10-2009
s/ *, */,/g
squish all '<space>,<space>' to ','

remove all trailing ',' from end of lines

s/,/ /2
substitute the SECOND occurrence of ',' with '<space>'

s/,/ /3
substitute the THIRD occurrence of ',' with '<space>'

/+/s/,/ /1
on all the lines having '+', substitute the FIRST occurrence of ',' with '<space>'

# 7  
Old 09-10-2009
Hey I have just seen your post.Thanx for your valuable time
You are mentioning 1st and sec occurrences...
The column may be contain so many numbers with commas. still does it work for all numbers??
like 1,2,3,4,5,6,7 and so on like in those columns I specified before?

I tried a sample its working fine for multiple numbers with commas also. I think its fine. What do you say?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

2. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

3. UNIX for Dummies Questions & Answers

sed help finding and editing

With sed 1. I need to find a line that contains "DVM" and "73069". 2. I need to insert a double quote at the beginning of the first line of the file. These two have been driving me crazy for the last 45 minutes. Any help would be greatly appreciated. Thanks (3 Replies)
Discussion started by: nlassiter
3 Replies

4. Windows & DOS: Issues & Discussions

Extracting variables between commas : GAWK or SED

Hello, I need some help, I got a CSV file called test.txt with this text in it : 08/02/2011;0,677;0,903;1,079;1,336;1,513;1,683 There's only a line and i need to copy theese numbers into variables : 0,677 0,903 1,079 1,336 1,513 1,683 The output file should look like this... (5 Replies)
Discussion started by: jujulips
5 Replies

5. UNIX for Dummies Questions & Answers

sed editing help....

Hello all, I need some help with sed. seems like i cant get through it. So here is what i am trying. when i do ps -ef|grep bla blah ...like below...i get /u01/app/oracle/11g/bin/tnslsnr .... but i want to replace that string with something using sed. So basically i want to get rid of... (3 Replies)
Discussion started by: abdul.irfan2
3 Replies

6. Shell Programming and Scripting

sed or awk editing help

Hi all I use aix (sadly). I've got a file consisting of fields separated by commas, I need a sed or awk command that will delete all spaces between two commas as long as there are only spaces between the commas. eg ,abc, ,sd , ,dr at would become ,abc,,sd ,,dr at I have... (53 Replies)
Discussion started by: mychmose
53 Replies

7. Shell Programming and Scripting

Help with sed and replacing white spaces with commas

Dear all, I am in a bit of a quandary. I have 400 text files which I need to edit and output in a very specific way. Here is a sample text file copied from gedit ... The columns will come out a bit messed up but when I cat <file>, it gives a table with six columns (0-28, tot lob vol, vcsf,... (6 Replies)
Discussion started by: antonz
6 Replies

8. Homework & Coursework Questions

String editing using sed? awk?

1. The problem statement, all variables and given/known data: Problem Statement for project: When an account is created on the CS Unix network, a public html directory is created in the account's home directory. A default web page is put into that directory. Some users replace or... (13 Replies)
Discussion started by: peage1475
13 Replies

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

10. Shell Programming and Scripting

Editing File using awk/sed

Hello Awk Gurus, Can anyone of you help me with the below problem. I have got a file having data in below format pmFaultyTransportBlocks ----------------------- 9842993 pmFrmNoOfDiscRachFrames ----------------------- NULL pmNoRecRandomAccSuccess -----------------------... (4 Replies)
Discussion started by: Mohammed
4 Replies
Login or Register to Ask a Question