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
# 8  
Old 03-29-2017
If the fields in your file are sometimes single-quoted and are sometimes unquoted and you want to print the selected lines from your file as they appear in the input file (obviously wild assumptions since no sample of the desired output has been provided), everything that you are doing seems extremely complicated when the task seems so simple. The fact that your awk samples are using undefined shell variables and that your awk code changes with every post contributes to the confusion.

Does the following (based on your code in post #5 in this thread) come close to what you're trying to do:
Code:
#!/bin/ksh
COLUMNANum=4
COLUMNANumVal=2
COLUMNBText=8
COLUMNBTextVal=EAI_ED

# Define variables that were undefined in original code...
SEPARATOR='|'
LOGFILE=${1:-datafile}

# Define awk variables from shell variables and run the code:
awk -v FANum="$COLUMNANum"	-v FAPat="^'?$COLUMNANumVal'?\$" \
    -v FBNum="$COLUMNBText"	-v FBPat="^'?$COLUMNBTextVal'?\$" \
    -F"$SEPARATOR" '
{	if($FANum ~ FAPat && $FBNum ~ FBPat) {
		print
		c++
	} else	o++
}
END {	# Note that w is never set, so there will always be 0 warnings.
	printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c)
}' "$LOGFILE"

Note that the patterns used for matching fields A and B (FAPat and FBPat, respectively) are anchored at both ends and allow 0 or 1 single quote at the beginning and end of the field being matched. With the sample data provided in post #1, this script produces the output:
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|
0:OK 0:WARNING 2:CRITICAL

As always, if you want to try this on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
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