Replace comma by space for specified field in record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace comma by space for specified field in record
# 1  
Old 04-19-2010
Replace comma by space for specified field in record

Hi,

i want to replace comma by space for specified field in record, i mean i want to replace the commas in the 4th field by space. and rest all is same throught the record.

the record is

Code:
16458,99,001,"RIMOUSKI, QC",418,"N",7,EST,EDT,902
16458,99,002,"CHANDLER, QC",418,"N",5,MST,MDT,616
16458,99,003,"NEWRICHMND, QC",418,"B",7,EST,EDT,702

here i want to replace "RIMOUSKI, QC" by "RIMOUSKI QC" and for all the records of the file in the 4th position.

i can do it for file using
Code:
sed 's/,//g'

but how can i do it only for 4th field ?

Last edited by Franklin52; 04-19-2010 at 06:44 AM.. Reason: Please use code tags!
# 2  
Old 04-19-2010
try this

Code:
sed 's/,/ /4' inputfile

# 3  
Old 04-19-2010
Code:
awk 'BEGIN{FS=OFS="\""}{sub(",",x,$2)}1' file

# 4  
Old 04-19-2010
Code:
sed 's/, / /g' filename

gives
PHP Code:
16458,99,001,"RIMOUSKI QC",418,"N",7,EST,EDT,902
16458
,99,002,"CHANDLER QC",418,"N",5,MST,MDT,616
16458
,99,003,"NEWRICHMND QC",418,"B",7,EST,EDT,702 
# 5  
Old 04-19-2010
Quote:
Originally Posted by devtakh
Code:
sed 's/, / /g' filename

That assumes there are no fields starting with blanks, that are one or more spaces, etc. I'm thinking the OP wants to remove commas from quoted fields, so this might be safer:

Code:
sed 's/"\([^"]*\),\([^"]*\)"/\1\2/g'

Edit: Ooops. Should have included double-quotes in parenthesis... :-)

Code:
sed 's/\("[^"]*\),\([^"]*"\)/\1\2/g'

js.

Last edited by jsmoriss; 04-19-2010 at 12:12 PM..
# 6  
Old 04-19-2010
Let's simulate:
Code:
# cat file
16458,99,001,"RIMOUSKI, QC",418,"N",7,EST,EDT,902
16458,99,002,"CHANDLER, QC",418,"N, C",5,MST,MDT,616
16458,99,003,"NEWRICHMND QC",418,"B, C",7,EST,EDT,702

# sed 's/,/ /4' file
16458,99,001,"RIMOUSKI  QC",418,"N",7,EST,EDT,902
16458,99,002,"CHANDLER  QC",418,"N, C",5,MST,MDT,616
16458,99,003,"NEWRICHMND QC" 418,"B, C",7,EST,EDT,702

# awk 'BEGIN{FS=OFS="\""}{sub(",",x,$2)}1' file
16458,99,001,"RIMOUSKI QC",418,"N",7,EST,EDT,902
16458,99,002,"CHANDLER QC",418,"N, C",5,MST,MDT,616
16458,99,003,"NEWRICHMND QC",418,"B, C",7,EST,EDT,702

# sed 's/, / /g' file
16458,99,001,"RIMOUSKI QC",418,"N",7,EST,EDT,902
16458,99,002,"CHANDLER QC",418,"N C",5,MST,MDT,616
16458,99,003,"NEWRICHMND QC",418,"B C",7,EST,EDT,702

# sed 's/"\([^"]*\),\([^"]*\)"/\1\2/g' file
16458,99,001,RIMOUSKI QC,418,"N",7,EST,EDT,902
16458,99,002,CHANDLER QC,418,N C,5,MST,MDT,616
16458,99,003,"NEWRICHMND QC,418B, C",7,EST,EDT,702

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 replace value in each field until a certain character in each record?

Each record coming with column names. I have to replace them in each record as shown below TIME=20181219110000261|CHAN=FMBKHJBAAAADPCFNAAAAAABA|EVNT=SWIclst|VALU=Session FMBKHJBAAAADPCFNAAAAAABA started|SRC=NSS|UCPU=0|SCPU=0 Output should look like: ... (9 Replies)
Discussion started by: sudhakar1987
9 Replies

2. Shell Programming and Scripting

Script using awk to replace space by comma

I have the content of this file that i would like to replace the space by comma. The file content values in this format FName LName Date & time ------------------------------------ Gilles John 14/12/17 12:30:45 I want this format Fname,LName,Date&time... (7 Replies)
Discussion started by: gillesi
7 Replies

3. Shell Programming and Scripting

Replace field in one file with whole record data of another

Hello Group, I need to replace the city field in “File 1 (fileld 3), with the entire record line of “File 2” (including delimiters) where the “city” field (File 1, Field 3)matches city field (File 2, Field1). All of the other data in “File 1” should remain intact(Fields 1,2,4,5,6). Only field... (1 Reply)
Discussion started by: vestport
1 Replies

4. Shell Programming and Scripting

replace 3rd field of space delimited text file

how to replace the 3rd colum? Each line begins similarly, but they all ends variously. XX YY 03 variable text here XX YY 03 more variable text here XX YY 03 even more variable text here really long setence XX YY 03 variable numbers also appear 03 11. 123 456 XX YY 03 the occasional comma,... (4 Replies)
Discussion started by: ajp7701
4 Replies

5. Shell Programming and Scripting

Replace comma with a blank space using SED

Hello everyone, I want to replace all "," (commas) with a blank space My command thus far is: cat test.text | sed -e s/\`//g | awk '{print$1" "$2" "$3}' I'm sure you guys know this, but the SED command that I am using is to get rid of the "`" (tics). which gives me: name ... (5 Replies)
Discussion started by: jayT
5 Replies

6. Shell Programming and Scripting

replace space with comma in perl

arr_Ent_NameId variable holds 'Prakash pawar' 'sag' '23' '50000' this value 'Prakash pawar' 'sag' '23' '50000' I want to replace space( ) with comma (,) There are 4 fields here. I don't want to replace first field with comma. output should be: 'Prakash,pawar','sag','23','50000' ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

7. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

8. Shell Programming and Scripting

How to replace all entries of comma in text file by space or other character

Hi , How to replace all entries of comma in text file by space or other character. cat temp.txt A,B,C,D I want this file to be like A B C D Please help!!! (4 Replies)
Discussion started by: prashant43
4 Replies

9. Shell Programming and Scripting

how to replace field for each record

Hello, I have the following xml formatted file. I would like to get the newnumber field number and replace into customernumber for each record. For example: <XMLFORMAT> <customernumberR11>9</customernumberR11> ... (12 Replies)
Discussion started by: happyv
12 Replies

10. Shell Programming and Scripting

Replace , (comma) with space

Hi, what is the better way to replace the , (comma) with a space char? Example:STRING=dir1,dir2,dir3 toSTRING=dir1 dir2 dir3 And.. how to find if in the string there is a comma? Thanks :) (6 Replies)
Discussion started by: mbarberis
6 Replies
Login or Register to Ask a Question