Visit Our UNIX and Linux User Community


awk logic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk logic
# 1  
Old 08-27-2013
Question awk logic

I am trying to check my logic on a long awk i'm using. I have about 30 checks that I built into an awk and I "believe" I did this right, but I could be wrong.

Code:
awk -F\| '
$9 !~ /\/*[0-9]{1,}*/
$9 ~ /\([A-Za-z]-[a-zA-Z0-9]{4}, [a-zA-Z0-9]{2,3}/
$9 ~ /\([A-Za-z0-9]{6}, [a-zA-Z0-9]{2,3}\)/
$9 ~ /\(\+[A-Za-z0-9]{5}, [a-zA-Z0-9]{2,3}\)/
$9 ~ /\(\+\+[A-Za-z0-9]{4}, [a-zA-Z0-9]{2,3}\)/
$9 ~ /\([A-Za-z0-9]{4}\+[A-Za-z0-9], [a-zA-Z0-9]{2,3}\)/
' file1.csv >> file2.csv

This is obviously just a subset to show the idea. My question is, when awk reads through my file, will it bail after the first match or will it continue down through each of my checks? Thanks!!
# 2  
Old 08-27-2013
It will run through all your checks, potentially printing the same line several times, and the first condition with the "not" will not stop it.

If you want this to be an or-condition, you could use an or-condition, like
Code:
($9 ~ /regex1) || ($9 ~ /regex2/) || ($9 ~ /regex3/)

to check multiple conditions but print only once.

How about this?

Code:
$9 !~ /\/*[0-9]{1,}*/ { next } # Skip lines containing  /91...
($9 ~ /regex1/) || ($9 ~ /regex2) ...' inputfile > outputfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-27-2013
So if I understand you correctly, if I want it to bail, I can just add
Code:
 {next}

to the end of each search string?
# 4  
Old 08-27-2013
By default, if you don't put { ... } after an expression, it assumes it should do { print }.

If you do { next } instead, it will skip to the next line without printing and start checking your expressions from the beginning instead.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-27-2013
Ok. Now what you said makes sense. I can skip the checks on records I don't want and then run all my checks after. Is there a limit on how many "||" I can do? Like I said, I have about 40 checks i'm doing per line.

Thank you so much for your helpful insights.
# 6  
Old 08-27-2013
Using or in regex
Code:
$9~/regex1|regex2|regex3|.../

This User Gave Thanks to Jotne For This Post:
# 7  
Old 08-27-2013
Quote:
Originally Posted by dagamier
Is there a limit on how many "||" I can do? Like I said, I have about 40 checks i'm doing per line.
Not really, but when you start piling them on like that it may be time to rethink your logic.

I forgot that awk supports | inside one regex like Jotne suggests, that will help.

Previous Thread | Next Thread
Test Your Knowledge in Computers #856
Difficulty: Medium
React was created by Jordan Renke, a software engineer at Facebook.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use this logic with awk?

Hi friends, I am having 2 files, I just want to compare 2 files each containing 2 columns 1st column is lat, and 2nd column is long, if anyone can understand below logic please help me in writing script with awk.. here each field of file2 needs to be compared with std_file main counter=0... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

2. UNIX for Dummies Questions & Answers

awk logic and math help

Hi, My file has 2 fields and millions of lines. variableStep chrom=Uextra span=25 201 0.5952 226 0.330693 251 0.121004 276 0.0736858 301 0.0646982 326 0.0736858 401 0.2952 426 0.230693 451 0.221004 476 0.2736858 Each field either has a... (6 Replies)
Discussion started by: wyarosh
6 Replies

3. UNIX for Dummies Questions & Answers

Need help in logic using awk command

I have task to find out the min,max, average value of each service for example i searched for " StatementService " $awk '/VST.*StatementService:/{print $3,$4,$19,$22,$25}' performance.log > smp.log $cat smp.log amexgtv VST: : StatementService:1860 StatementService:getCardReference:0... (3 Replies)
Discussion started by: senthil.ak
3 Replies

4. Shell Programming and Scripting

need an awk script/logic

In one data file i have values like this a b c 1 2 e f g 2 3 i j k 3 5 I need to sum up the last 2 columns and make a data file...How i can do that. a b c 1 2 e f g 2 3 i j k 3 5... (8 Replies)
Discussion started by: bobprabhu
8 Replies

5. Shell Programming and Scripting

Help with awk logic

I want to print lines that have "IND" or "ind" or nothing in field 2 or 3 file: output needed: Code i wrote: nawk -F"," '{if(tolower($2||$3) ~"ind"||"")print}' file Help is appreciated (3 Replies)
Discussion started by: pinnacle
3 Replies

6. Shell Programming and Scripting

need a logic to start with awk/ sh

Hi Friends, I got stuck where to start with .. I ve a input file like below. where I want to compare write data with my read data .. The problem is that the read data should be compared with the lastest write data on that address. Note- Both write data & read data are in the same... (8 Replies)
Discussion started by: user_prady
8 Replies

7. Shell Programming and Scripting

need a logic for awk programming

Hello Friends, I have a txt file like below //*Init Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Init End //*Main Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Main End I want to calculate the values between //* Init Start & //* Init End And //*Main Start & //*Main... (5 Replies)
Discussion started by: user_prady
5 Replies

8. Shell Programming and Scripting

cannot get logic for concatenation awk

Hello friends, I have a problem in printing an array.. Example if my array line contains 4 elements like following line=0002 , line=202200, line=200002, line= 300313 Now one = sprintf line line line line will concatenate my whole array to one. But I am not sure about the... (7 Replies)
Discussion started by: user_prady
7 Replies

Featured Tech Videos