Print line if values in fields matches number and text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print line if values in fields matches number and text
# 1  
Old 03-28-2017
Print line if values in fields matches number and text

datafile:
Code:
2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS      00040      /ZZ Appl:PF Func:PD Txn:832    Group Cntr:None ISA CntlNr:None Ver:003050      '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog|
2017-03-27 02:50:02.028706|5|'No Route for Sndr:RETEK RMS      00040      /ZZ Appl:PF Func:PD Txn:832    Group Cntr:None ISA CntlNr:None Ver:003050      '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139188896|ActivityLog|

I need a awk command that will output the lines from a log, if the number in column 4 is equal to 2 AND the text in column 8 contains "EAI_ED". The fields from the log are separated by Pipes.

here's my attempt awk attempt:

Code:
COLUMNANum=4
COLUMNANumVal=2

COLUMNBText=8
COLUMNBTextVal=EAI_ED

awk -F"|" '{split($0,a,"|"); gsub("\'", "", a[1]);
        if ( $'$COLUMNA' ~ a[1]) &&  ( $'$COLUMNB' ~ a[8]) 
                { print ; w++ }
        else if ($'$COLUMNA' ~ '$CRIT')
                { print ; c++ }
        else { o++}} END {
                printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c)
        }' ${LOGFILE}

# 2  
Old 03-28-2017
How about
Code:
awk -F"|" '

($C1 == V1) && ($C2 ~ V2)

' C1="$COLUMNANum" V1="$COLUMNANumVal" C2="$COLUMNBText" V2="$COLUMNBTextVal"   file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-28-2017
Quote:
Originally Posted by RudiC
How about
Code:
awk -F"|" '

($C1 == V1) && ($C2 ~ V2)

' C1="$COLUMNANum" V1="$COLUMNANumVal" C2="$COLUMNBText" V2="$COLUMNBTextVal"   file

this doesnt seem to work and i think its because there is single quotes surrounding number in column 4.
# 4  
Old 03-28-2017
Try to relieve the condition a bit: $C1 ~ V1 (may yield false positives) or put regex differently $C1 ~ "^."V1".$"
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-28-2017
it loooks like im still having problems with this.

considering this particular code is part of a larger work that im working on, i need to be able to accomplish what i need using the following line of thinking:

Code:
COLUMNANum=4
COLUMNANumVal=2
COLUMNBText=8
COLUMNBTextVal=EAI_ED

awk -F"${SEPARATOR}" -v STRING="EAI_ED" '{split($0,a,"|"); gsub("'\''", "", a[8]) ; gsub("'\''", "", a[4])
        if (( a['$COLUMNANum'] == '${COLUMNANumVal}' ) && ( STRING ~ a['${COLUMNBText}'] ))
                   { print ; c++ }

        else
                { o++ }
        } END {
                printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c)
        }' ${LOGFILE}

The above works but when I add bogus text to the end of text "EAI_ED" like this "EAI_EDi", the code still outputs the lines.

i believe my problem is in the bolded. can someone help me straighten this out?

Last edited by SkySmart; 03-28-2017 at 04:14 PM..
# 6  
Old 03-28-2017
Why that crooked logics? What advantages do you expect from splitting the line into that a array over using the respective fields directly? Why do you pass STRING the correct way and not the other variables?
The ~ operator matches occurrences of substrings, so an concatenated "i" won't stop the matching.
# 7  
Old 03-28-2017
Quote:
Originally Posted by RudiC
Why that crooked logics? What advantages do you expect from splitting the line into that a array over using the respective fields directly? Why do you pass STRING the correct way and not the other variables?
The ~ operator matches occurrences of substrings, so an concatenated "i" won't stop the matching.
im doing the splitting because, the column that contains the value i need have single quotes in them. so i needed a way to get rid of the single quotes from the specific column i was interested in.

i apologize if im making this necessarily complicated. if i can resolve the issue i bolded, i can move on from this problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

3. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Print whole line if variables matches

Der colleagues, 4 days I am trying to solve my issue and no success.. Maybe you can give me a clue how to achieve what I need.. So I have two files. file1 example: 1_column1.1 1_column2.1 aaa 1_column4.1 1_column1.2 1_column2.2 ttt 1_column4.2 1_column1.3 1_column2.3 ... (10 Replies)
Discussion started by: nypreH
10 Replies

5. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

6. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

7. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

8. Shell Programming and Scripting

print the number of fields in each line

I am looking for equivalent of following awk command in perl # awk '{ print NF ":" $0 } ' junk1 8:VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP DESCR 8:2 NIC6 cont 34 y n shal_orgrp /shal 8:4 NIC5 signa 52 n y shal_orgrp... (3 Replies)
Discussion started by: dynamax
3 Replies

9. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

10. Shell Programming and Scripting

Get line number when matches a string

If I have a file something like as shown below, ARM*187878*hjhj BAG*88778*jjjj COD*7777*kkkk BAG*87878*kjjhjk DEF*65656*89989*khjkk I need the line numbers to be added with a colon when it matches the string "BAG". Here in my case, I need something like ARM*187878*hjhj... (4 Replies)
Discussion started by: Muthuraj K
4 Replies
Login or Register to Ask a Question