Computing.Net > Forums > Unix > matching first 2 char in a word

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.

matching first 2 char in a word

Reply to Message Icon

Name: dips
Date: August 20, 2007 at 23:15:53 Pacific
OS: sunos.5.8/8.7
CPU/Ram: Intel & 512 MB RAM
Product: Pentium(R) 2.66 GHz
Comment:

I want to select the word only if starts with 'LN'. So I used awk '/LN/ {print $1}' but this returns other words too with 'LN' anywhere except the first two char like 'NY00xxLN' which I don't want. How to tell it to match only first 2 char in the word??


dips



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: August 21, 2007 at 09:03:08 Pacific
Reply:

Here is a basic loop to look at each word in the line.  I don't think you are interested in locating multiple qualifying words on the same line, so upon encountering the first qualifying word, I break out of the for-loop.

I used the "like" operator and beginning-of-expression anchoring, but you could also use if (substr($w,1,2)=="LN")

awk '{
for (w=1;w<=NF;w++)
   if ($w~"^LN")
      {print "whatever ..."
       break}
}' infile


0

Response Number 2
Name: dips
Date: August 21, 2007 at 23:52:00 Pacific
Reply:

Thanks...
That didn't work. I tried with nawk also but it says Syntax error....But i tried this
$nawk -F"," '$2~/^LN/{print $2}' infile.txt
It works... I only want "the searching" to be done in the 2nd field of the file


dips


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: matching first 2 char in a word

detect a char in a string (ksh) www.computing.net/answers/unix/detect-a-char-in-a-string-ksh/4896.html

AWK command to find 2 values in a f www.computing.net/answers/unix/awk-command-to-find-2-values-in-a-f/5853.html

Tell Numeric from Char in Unix script www.computing.net/answers/unix/tell-numeric-from-char-in-unix-script/3666.html