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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk script for search and replace

Reply to Message Icon

Name: htnkd
Date: July 26, 2005 at 07:38:25 Pacific
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




Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: July 26, 2005 at 09:14:08 Pacific
Reply:

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


0

Response Number 2
Name: htnkd
Date: July 26, 2005 at 10:41:20 Pacific
Reply:

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


0

Response Number 3
Name: Jim Boothe
Date: July 26, 2005 at 11:12:34 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk script for search and replace

Unix - Search and replace www.computing.net/answers/unix/unix-search-and-replace/6775.html

Search And Replace With Sed www.computing.net/answers/unix/search-and-replace-with-sed/3845.html

Help search and replace www.computing.net/answers/unix/help-search-and-replace/6886.html