AWK match and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK match and print
# 1  
Old 06-15-2011
AWK match and print

I have thousands of tables compiled in a single txt document that I'm parsing with AWK. Scattered throughout the document in random sections I would like to parse out the sections that look like this:

Code:
1	Seq.	Descrição do bem	Tipo do bem	Valor do bem (R$)
2	1	LOCALIZADO ANA RUA PESSEGO N 96 BAIRRO MORADA DO SOL	Apartamento	58.870,00
3	2	LOCALIZADA A AV MANCIO LIMA MUNICIPIO DE CRUZEIRO DO SUL MEDINDO 400M2	Casa	200.000,00
4	3	LOCALIZADO A AVENIDA 15 NOVEMBRO NO MUNICIPIO DE CRUZEIRO DO SUL MED 15X30	Terra nua	35.575,50
5	4	TOYOTA HILUX ANO 2009	Ve*culo automotor terrestre: caminhão, automóvel, moto, etc.	120.000,00
6	5	FAZENDA CALIFORNIA II 850HEC LOCALIZADA A ESTRADA BADEJO KM18 MUN CRUZEIRO DO SUL	Outros bens imóveis	400.000,00

I've decided it is best to parse based on the $2 field since other sections begin with numbers however do not contain the data I desire from $3 $4 $5, as is the case with the snippet above. I'm looking to match lines based on $2 in the numerical range of 1-35 then print $3, $4, $5 of those matching lines.

What I currently have which doesn't quite accomplish it:
Code:
{if ($2 >= 2 && $2 <= 31) {print $3, $4, $5}continue;}

I assume I could also use regular expressions to match based on $2:
Code:
{if ( $2 ~ /^[1-31]$/ ) {print ....

I would prefer it to break after range and resume the script code from the top but not sure how to do that...
# 2  
Old 06-15-2011
What you are trying to do makes no sense to me...can you provide a better example.
# 3  
Old 06-15-2011
A couple of things that might be causing your problems...

First, you say that you want to print the desired fields when field 2 is between 1 and 35 (inclusive) yet your if statement prints only when less than or equal to 31. There's a typo in your code, or your problem statement.

Secondly, some of your values in column 2 are not numeric. I have seen some versions of awk do funny things with an expression like

Code:
if( $2 <= 35 ))

when the value of $2 is something like "foo" rather than a number. To prevent this from happening, I generally write the expression like this:

Code:
if( $2+0 <= 35 )

This forces awk to convert the value of $2 into a floating point before the comparison is made. If the value of $2 is not numeric, it will convert to 0 (zero), and something like 123abc will convert to 123.

Neither of these may be your problem, but without an example of what you are getting as output and what you are expecting it is difficult to guess.
# 4  
Old 06-16-2011
I actually realized that the data that I needed was encapsulated between a 'Seq.' and 'Total:' fields every time:

Code:
1	Seq.	Descrição do bem	Tipo do bem	Valor do bem (R$)
2	1	LOCALIZADO ANA RUA PESSEGO N 96 BAIRRO MORADA DO SOL	Apartamento	58.870,00
3	2	LOCALIZADA A AV MANCIO LIMA MUNICIPIO DE CRUZEIRO DO SUL MEDINDO 400M2	Casa	200.000,00
4	3	LOCALIZADO A AVENIDA 15 NOVEMBRO NO MUNICIPIO DE CRUZEIRO DO SUL MED 15X30	Terra nua	35.575,50
5	Total:	65000	NA	NA

I so I searched based on that:
Code:
/Seq./,/Total:/

Now I'm looking to insert a new line after each of scrapes...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to match string and print status

Dear team, Need support to built awk script for below requirement Input file LOTC cluster state: ------------------- Node safNode=SC_2_1 joined cluster | Node safNode=SC_2_2 joined cluster | Node safNode=PL_2_3 fail cluster | AMF cluster state: ------------------... (16 Replies)
Discussion started by: shanul karim
16 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk Match and Print

I have the following script in place that will print the values of FileB when the first column matches File A's first column. awk 'NR == FNR {A=$2;next};$1 in A {print $1,$NF,$2,$3,A}' FileA FileB Input FileA 3013 4 FileB 3013 2009 03 JUNK 43 Output 3013 43 2009 03 (2 Replies)
Discussion started by: ncwxpanther
2 Replies

4. Shell Programming and Scripting

Using awk for match and print

I have the need to match up the lat / lon from a fileA with the lat / lon and value from fileB. fileA is a small subset of fileB I have the following awk script but it prints out all the contents from fileB. I only need the matches. awk 'FNR==NR {A=$NF; next} {A=$NF} END{for(i in A) printf... (10 Replies)
Discussion started by: ncwxpanther
10 Replies

5. Shell Programming and Scripting

awk script to match and print

I need a script that will search for a string from column 1 in file A and when the string matches the last column in file B, print columns 1, 2 (file A) and columns 2, 3 (file B). input file A stringtomatch1 a stringtomatch2 a stringtomatch3 b file B junkcolumn1 printcolumn2... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

6. Shell Programming and Scripting

awk if condition match and print all

Hi, I am trying to do something like this ... I use awk to match a pattern, and then print out all col. My code is : awk '{if ($1 ==300) print $1,$2-'$sbin7',$3}' tmp.txt output= 300 2 whereby sbin7=2, The thing is, I want to print all col and row, not just the matched line/row only, but... (10 Replies)
Discussion started by: horsepower
10 Replies

7. Shell Programming and Scripting

awk Help: Horizontal to Vertical print with pattern match

Hi AWK Experts, Following is the data : BRH113 DD AA HH CA DD DD AA HH BRH091 A4 A6 AH H7 67 HH J8 9J BRH0991 AA D8 C23 V5 H7 BR2 BRH991 AA HH GG5 BT0 JJ0 I want the output to be alligned with the pattern matching "BRH" inthe line. The output should be look like: A]... (4 Replies)
Discussion started by: rveri
4 Replies

8. Shell Programming and Scripting

Print strings that match pattern with awk

I have a file with many lines which contain strings like .. etc. But with no rule regarding field separators or anything else. I want to print ONLY THE STRING from each line , not the entire line !!! For example from the lines : Flow on service executed with success in . Performances... (5 Replies)
Discussion started by: black_fender
5 Replies

9. UNIX for Dummies Questions & Answers

Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches? I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following... (9 Replies)
Discussion started by: MrEddy
9 Replies

10. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies
Login or Register to Ask a Question