Computing.Net > Forums > Unix > awk script for search and replace

awk script for search and replace

Reply to Message Icon

Original Message
Name: htnkd
Date: July 26, 2005 at 07:38:25 Pacific
Subject: awk script for search and replace
OS: xp
CPU/Ram: 1G
Comment:

I have a file with thousands of records in it.
Each record in the file being of a fixed length of 190 bytes.

Example:
0003006315B000000002100601X646000000054717154-12700911700 00Telkwa Trailer Park
0003006317B000000003000601X646000000054717154-12700911700 00Tyhee Trailer Park
0003006613B000000002000601X646000000053242950-13181814600 00Beban Trailer Park
0003006848B000000006000601X646000000054163445-12542196600 00Freeport Trailer Park
What I would like to do is for each record position 11 == B and somewhere starting at positon 80 for 49 bytes there is the word "Trailer". I want to replace position 28 for 3 with "xxx" for that record (Example X646 would become Xxxx). I want to do this for the entire file.

any help on how to get this to work. I am new to this and I have been doing sed on each individual record and this would take forever.

Thanks for any help in advance



Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: July 26, 2005 at 09:14:08 Pacific
Subject: awk script for search and replace
Reply: (edit)

awk '\
{ if ( substr($0,11,1)=="B" \
  &&   match(substr($0,61),"Trailer") )
     print substr($0,1,27) "xxx" substr($0,31)
  else
     print
}' filein > fileout


Report Offensive Follow Up For Removal

Response Number 2
Name: htnkd
Date: July 26, 2005 at 10:41:20 Pacific
Subject: awk script for search and replace
Reply: (edit)

thanks--
if you have a word with "No." how can I get a search with the "." instead of everything with "No"

thanks for the help above


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: July 26, 2005 at 11:12:34 Pacific
Subject: awk script for search and replace
Reply: (edit)

For example, in the two lines below, you want only the first line:

No. America
North America

In regular expressions, a dot means any single character. You need to precede the dot with a backslash which will "escape" its special meaning and be taken as an actual dot:

grep "No\." myfile

awk '/No\./' myfile

awk '{if (match($0,"No\."))print}' myfile


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: awk script for search and replace

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software