Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi,
i want to check for a field on file and if the field from 670 to 672 is TX,OK,WA then i need to write those lines in a new file , if not it should be written in another file. i tried the below one but its not working...
can someone look for me???cat text.txt | awk '{if ((substr($0,670,2) != "TX") and (substr($0,670,2) != "OK") and (substr($0,670,2) != "WA")) print ($0) }' >> /tmp/others.txt
cat text.txt | awk '{if ((substr($0,670,2) == "TX") or (substr($0,670,2) == "OK") or (substr($0,670,2) == "WA")) print ($0) }' >> /tmp/txokwa.txt
gobi

In awk, use || for the or operator and && for the and operator.
awk can output to multiple files at the same time, so no need to do two passes.
Since you are appending, keep in mind that you would append to any existing files. If you do not want to append to yesterday's files, then do the rm.
Finally, you do not need to cat the file into awk, which just creates an extra process. awk can open and read the input file itself.
#rm /tmp/txokwa.txt /tmp/others.txtawk '
{if ((substr($0,670,2) == "TX") || (substr($0,670,2) == "OK") || (substr($0,670,2) == "WA"))
print $0 >> "/tmp/txokwa.txt"
else
print $0 >> "/tmp/others.txt"
}' text.txt

![]() |
Kill process script
|
List of changed files in ...
|

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