Replacing underscore with white space after comma.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing underscore with white space after comma.
# 1  
Old 06-22-2009
Replacing underscore with white space after comma.

Hi all,
I'm hoping you can help.. I've used this forum a couple of times and I am back again now i've moved onto something more complex (for me!)

I have some data which looks like:
"AL1_1,AL1_1,"
"AL1_1.AL1_1A.AL1_1AE,AL1_1AE,"
"AL1_1.AL1_1A.AL1_1AG,AL1_1AG,"
"AL1_1.AL1_1A.AL1_1AJ,AL1_1AJ,"
"AL1_1.AL1_1B.AL1_1BH,AL1_1BH,"

What i need it to look like is:
"AL1_1,AL1 1,"
"AL1_1.AL1_1A.AL1_1AE,AL1 1AE,"
"AL1_1.AL1_1A.AL1_1AG,AL1 1AG,"
"AL1_1.AL1_1A.AL1_1AJ,AL1 1AJ,"
"AL1_1.AL1_1B.AL1_1BH,AL1 1BH,"

So all underscores after the comma need to be replaced with a white space.. Is this possible at all?

Many thanks for reading Smilie
Gareth
# 2  
Old 06-22-2009
Code:
sed -e "s/_/ /"
 
 
"AL1_1,AL1 1,"
"AL1_1.AL1_1A.AL1_1AE,AL1 1AE,"
"AL1_1.AL1_1A.AL1_1AG,AL1 1AG,"
"AL1_1.AL1_1A.AL1_1AJ,AL1 1AJ,"
"AL1_1.AL1_1B.AL1_1BH,AL1 1BH,"

# 3  
Old 06-22-2009
Thanks Scott!
Will this work on the file which 500,000 rows? Can I call the file form the command line?

---------- Post updated at 12:02 PM ---------- Previous update was at 12:00 PM ----------

Ive just tried
Code:
sed -e "s/_/ /" regions_postcodes.dat > region_postcodes1.dat

However, it stripped all underscores, not just the ones after the comma. Smilie
# 4  
Old 06-22-2009
Try this:

Code:
awk -F, '{gsub("_"," ",$2)}1' OFS="," file > newfile

# 5  
Old 06-22-2009
It doesn't replace all underscores... only the last one.

Based on the data you provided and the "expected output" sed -e "s/_/ /" does what you asked.

Last edited by Scott; 06-22-2009 at 08:24 AM..
# 6  
Old 06-22-2009
Unfortunately i've got the following error after running:
Code:
awk -F, '{gsub("_"," ",$2)}1' OFS="," file > newfile

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
# 7  
Old 06-22-2009
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a space with underscore in a file

i have a file with following data. { EqName "Tan 1" .... .... } { EqName "Sin 2" ... ... } I have to replace the value of EqName to Tan_1 and Sin_2 in file.Can i use sed or awk ? cat file|grep EqName|awk '{print $2 $3}'|sed -i 's//_/g' I tried with this but it... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

3. Shell Programming and Scripting

Replacing white spaces in filename

Hi; In following code find LOG_DIR -type f | while read filename; do echo $filename; done I want to precede each white space encountered in filename with \ so that when i use $filename for running some commands in do...done,it wont give me an error. will appreciate ur help in this.... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

4. Shell Programming and Scripting

Using of gsub function in AWK to replace space by underscore

I must design a UNIX script to monitor files whose size is over a threshold of 5 MB in a specific UNIX directory I meet a problem during the for loop in my script. Some file names contain spaces. ls -lrt | awk '$5>=5000000 && length($8)==5 {gsub(/ /,"_",$9); print};' -rw-r--r-- 1 was61 ... (2 Replies)
Discussion started by: Scofield38
2 Replies

5. Shell Programming and Scripting

sed help - replacing 6th comma with a space

Hi, How can I replace the 6th comma on each line (of a csv) with a space? Any online tutorials with plenty of examples using sed would be very useful. Alex (2 Replies)
Discussion started by: mcclunyboy
2 Replies

6. Shell Programming and Scripting

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 Replies

7. Shell Programming and Scripting

Suppressing space replacing by comma

hi i want to replace spaces by comma my file is ADD 16428 170 160 3 WNPG 204 941 No 204802 ADD 16428 170 160 3 WNPG 204 941 No 204803 ADD 16428 170 160 3 WNPG 204 941 No 204804 ADD... (9 Replies)
Discussion started by: raghavendra.cse
9 Replies

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

9. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

10. Shell Programming and Scripting

Regex/sed - matching any char,space,underscore between : and /

trying to remove the portion in red: Data: mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql output to be: mds_ar/bin/uedw92wp.ksh: wkly.sql mds_ar/bin/uedw92wp.ksh: wkly.sql SED i'm trying to use: sed 's/:+\//: /g' input_file.dat >... (11 Replies)
Discussion started by: danmauer
11 Replies
Login or Register to Ask a Question