help with search and replace in multiple fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with search and replace in multiple fields
# 1  
Old 07-08-2010
help with search and replace in multiple fields

I have a pipe delimited file with 27 fields. Each record has 26 fields. I need to search for the 25,26,27 fields and replace "," with nothing.

How can I acheive this. Sed is more preferred.

e.g

data row

Quote:
19|147444|3006266|Sponsorships 001|1869440|070210||Colorado Springs |300x100|B4098575.31|100|Scion|Da|Da|1,491|0|0.0|Clicks|2010-07-03|0|1,491|0|0|Y|1,566|0|7,32011
o/p
Quote:
19|147444|3006266|Sponsorships 001|1869440|070210||Colorado Springs |300x100|B4098575.31|100|Scion|Da|Da|1,491|0|0.0|Clicks|2010-07-03|0|1,491|0|0|Y|1566|0|732011
# 2  
Old 07-08-2010
Code:
awk -F"|" -vOFS="|" '{sub(",","",$25);sub(",","",$26);sub(",","",$27);print}' file

# 3  
Old 07-08-2010
Hi.

Not sure that sed is the best for that.

Code:
$ awk 'BEGIN { OFS=FS="|" } 'function GSUB( F ) { gsub( ",", "", $F ) } { GSUB( 25 ); GSUB( 26 ); GSUB( 27 ) } 1' file

19|147444|3006266|Sponsorships 001|1869440|070210||Colorado Springs |300x100|B4098575.31|100|Scion|Da|Da|1,491|0|0.0|Clicks|2010-07-03|0|1,491|0|0|Y|1566|0|732011

# 4  
Old 07-08-2010
scottn,

I am getting below error with your command

Quote:
-ksh: syntax error: `(' unexpected


---------- Post updated at 01:42 PM ---------- Previous update was at 01:40 PM ----------

got it. figured out

Quote:
awk 'BEGIN { OFS=FS="|" } function GSUB( F ) { gsub( ",", "", $F ) } { GSUB( 25 ); GSUB( 26 ); GSUB( 27 ) } 1' file
# 5  
Old 07-08-2010
Quote:
Originally Posted by dsravan
scottn,

I am getting below error with your command
Ooh! Me too Smilie

Don't know what happened there... back to the original:

Code:
$ awk -F\| -v OFS=\| 'function GSUB( F ) { gsub( ",", "", $F ) } { GSUB( 25 ); GSUB( 26 ); GSUB( 27 ) } 1' file1       
19|147444|3006266|Sponsorships 001|1869440|070210||Colorado Springs |300x100|B4098575.31|100|Scion|Da|Da|1,491|0|0.0|Clicks|2010-07-03|0|1,491|0|0|Y|1566|0|732011

# 6  
Old 07-08-2010
Thanks Scottn and bartus
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

2. Shell Programming and Scripting

Search and Replace in multiple files

Hello, I have hundreds of files in which I need to change email address. Here is what I am trying to do: 1. All text files are in a directory "a" 2. In the text file, I want to replace email address for preparer. All these lines start with {{PreparerEmail and end with }}. The email... (3 Replies)
Discussion started by: cartrider
3 Replies

3. Shell Programming and Scripting

Perl search multiple fields

Hi all, I have a flatfile 300 lines tiger,tampa10-pc,yellow,none,2013-02-25 08:56:51.000,2013-02-25 21:41:11.380,12hrs : 44min cat,tampa10-pc,white,none,2013-02-28 08:56:58.000,2013-03-04 23:18:23.003,110hrs : 21min dog,tampa10-pc,yellow,none,2013-03-05 09:50:17.000,2013-03-07... (5 Replies)
Discussion started by: sabercats
5 Replies

4. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

5. Shell Programming and Scripting

perl: search replace in multiple files

When I use special characters the command to replace multiple files with a string pattern does nt work. ---------- Post updated at 12:33 PM ---------- Previous update was at 11:38 AM ---------- This works perl -pi -e 's/100/test/g' * This does nt work perl -pi -e 's... (1 Reply)
Discussion started by: w020637
1 Replies

6. Shell Programming and Scripting

Find and Replace in multiple fields using awk

Hi, Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>. Input: "1|22| | |" --> "1|22|<space> |<space> |" Expected output: "1|22|||" --> "1|22|<null> |<null>|" I tried: echo "1|22| | |" | awk -F... (4 Replies)
Discussion started by: machomaddy
4 Replies

7. Shell Programming and Scripting

Search & replace fields from file1 to file2

hi, I have two xml files with the name source.xml and tobe_replaced.xml. Sample data: source.xml contains: <?xml version="1.0"?> <product description="prod1" product_info="some/info"> <product description="prod2" product_info="xyz/allinfo"> <product description="abc/partialinfo"... (2 Replies)
Discussion started by: dragon.1431
2 Replies

8. UNIX for Dummies Questions & Answers

multiple input search and replace script

hi, i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files. cho "" echo... (3 Replies)
Discussion started by: tungaw2004
3 Replies

9. UNIX for Dummies Questions & Answers

search and replace different fields

Hi i want to search and replace different field on each files. file 1 FIELD2=xxxxxxxxx FIELD4=xxxxxxxx FIELD3=xxxxxxx FIELD1=20000 file 2 FIELD1= FIELD2= file 3 FIELD2=xxxxxxxxx (3 Replies)
Discussion started by: tungaw2004
3 Replies

10. UNIX for Dummies Questions & Answers

Multiple search and replace

Hi, I have different files and I want to automatically change the values of the defined variables. i want to get rid of editing all the files and make it a little bit faster. my problem is like this. 1. i will input all the new values. 2. substitute this values into the values inside the... (1 Reply)
Discussion started by: tungaw2004
1 Replies
Login or Register to Ask a Question