Search for a substr with nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a substr with nawk
# 1  
Old 03-26-2013
Search for a substr with nawk

Hi, I have files, with fixed length fields/let's say every field 5 positions/, like this:
Code:
xxxx 140  xxxxx
xxxx 140  xxxxx
xxxx 1400 xxxxx
xxxx 150  xxxxx

I need to get only the records, which have 140 in the second column. I use that command:
Code:
nawk '{if (substr($0,6,3)=="140") print $0}' input_files >> ooutput_file;

But that also return me the rows that contain 1400.
Any idea how to fix that?

Thanks!
# 2  
Old 03-26-2013
You don't need to use substrings, awk can handle columns all on its own.
Code:
awk '$2 == "140"' inputfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-26-2013
Well, if your file does actually consist of fixed length fields, then the length of the substring extracted and the length of the string literal used for comparison should both be equal to the length of the field.

If, however, your file does not consist of fixed fields, and is instead delimited by whitespace or some other field separator, then Corona has already given you the correct solution.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 03-26-2013
Quote:
Originally Posted by Corona688
You don't need to use substrings, awk can handle columns all on its own.
Code:
awk '$2 == "140"' inputfile

Thanks Corona688, it works perfect!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search comppare replace (substr/lppad)

Hi Everyone, I have a data file with data as below which contains millions of data and gets loaded to database using SQL loader using positional notation. AT 0001 000000000100000000000 BE 4 000000000000030000000 DE 0055 000004000000000000000 FR 0 ... (8 Replies)
Discussion started by: Showdown
8 Replies

2. UNIX for Dummies Questions & Answers

Substr

awk '/^>/{id=$0;next}length>=7 { print id, "\n"$0}' Test.txt Can I use substr to achieve the same task? Thanks! (8 Replies)
Discussion started by: Xterra
8 Replies

3. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

4. Shell Programming and Scripting

search pattern that has spaces with nawk

Hi guys, I need a help ! I need do grab some string from file and then count n lines after that pattern. This is working fine, but my problem is that the string to be searched has spaces within, like an example : LINK COUNTERS what I am using is: nawk... (2 Replies)
Discussion started by: robdcb
2 Replies

5. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

6. UNIX for Dummies Questions & Answers

substr

can anybody explain this code? thanks in advance..:) (6 Replies)
Discussion started by: janani_kalyan
6 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Shell Programming and Scripting

get substr?

Hi, I have a long string like, aabab|bcbcbcbbc|defgh|paswd123 dedededede|efef|ghijklmn|paswd234 ghghghghgh|ijijii|klllkkk|paswd345 lmlmlmmm|nononononn|opopopopp|paswd456 This string is devided into one space between substrings. This substrings are, aabab|bcbcbcbbc|defgh|paswd123... (6 Replies)
Discussion started by: syamkp
6 Replies

9. UNIX for Dummies Questions & Answers

Substr

Hi, My input file is 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... .. .. I need to change the second field value from 2 to 1. i.e., 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... .. .. Thanks in advance. (9 Replies)
Discussion started by: deepakwins
9 Replies

10. Shell Programming and Scripting

Using substr

What is the more efficient way to do this (awk only and default FS) ? $ echo "jefe@alm"|awk '{pos = index($0, "@");printf ("USER: %s\n",substr ($0,1,pos-1))}' USER: jefe Thx in advance (2 Replies)
Discussion started by: Klashxx
2 Replies
Login or Register to Ask a Question