Computing.Net > Forums > Unix > Help in AWK

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.

Help in AWK

Reply to Message Icon

Name: gobi (by gp12)
Date: January 29, 2008 at 08:30:40 Pacific
OS: UNIX
CPU/Ram: -/512
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: January 29, 2008 at 09:56:21 Pacific
Reply:

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

awk '
{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


0

Response Number 2
Name: gobi (by gp12)
Date: March 23, 2008 at 01:12:34 Pacific
Reply:

James,

Thanks for your detailed reply. It worked out.

gobi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Kill process script List of changed files in ...



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: Help in AWK

URGENT! help with sorting in awk www.computing.net/answers/unix/urgent-help-with-sorting-in-awk/4442.html

sorting command problem in awk www.computing.net/answers/unix/sorting-command-problem-in-awk-/3552.html

Shell variable in AWK www.computing.net/answers/unix/shell-variable-in-awk/6651.html