awk command to display particular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to display particular pattern
# 8  
Old 05-03-2018
Quote:
Originally Posted by Don Cragun
The two commands:
Code:
cat /tmp/OutFile | egrep "GAP" | awk '{print $10, $11}' | sort| uniq >> ${FILE}

cat /tmp/OutFile | egrep "DUP" | awk '{print $11, $12}' | sort| uniq >> ${FILE}

clearly can't produce the desired output since you are printing two whitespace-separated input fields when your output contains three whitespace-separated fields. Furthermore, you have two unneeded invocations of cat, egrep, and uniq; one unneeded invocation of awk and one or two unneeded invocations of sort.

The following is an alternative to the code RudiC suggested that seems to do what you want with one invocation of awk and one invocation of sort:
Code:
awk '
/DUP/ { gsub(/[()]/, "", $13)
        out[$11 " " $12 "\t" $13]
}
/GAP/ { gsub(/[()]/, "", $12)
        out[$10 " " $11 "\t" $12]
}
END {   printf("1st column\t2nd column\n")
        for(line in out) 
                print line | "sort"
}' file

which (if file contains one or more copies of the three sample input lines you showed us in post #1) produces the output:
Code:
1st column	2nd column
CL/U18 9P-NC	CL90U8
CL/X18 8.5P-NC	CL85W8

which contains a space separating the two input fields that make up the "1st column" output and a tab separating that from the single output field that makes up the "2nd column" output.

If you don't care about the order of lines in the output (and were just using sort | uniq to get rid of duplicate lines of output instead of caring about the order of the output, delete the text in the script shown in red and it will run a little bit faster.

If you are trying this on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk or nawk.

It isn't clear to me whether you actually want those headings or not, but I included them since that is what you said you wanted. I assume that it is obvious that you can remove the printf statement from my script above if you don't want the heading line in your output.
Thanks, Don for the detailed explanation.
I am getting the following error

Code:
awk: syntax error near line 2
awk: illegal statement near line 2
awk: syntax error near line 5
awk: illegal statement near line 5

---------- Post updated at 12:26 PM ---------- Previous update was at 12:22 PM ----------

Hi RudiC,
I see the command stopped producing the desired output.

File has
Code:
seq 751287| Thu May 03 15:21:57.526253 2018|Len: 77|GAP Ric IOVVA.Z Flag2 14 Sym IOBVA-I msg SEQ_NO:2628 db:2626 diff 2 process msg||
seq 751288| Thu May 03 15:21:57.542730 2018|Len: 77|GAP Ric IOGNS.B Flag2 17 Sym IOGNS-J msg SEQ_NO:1375 db:1373 diff 2 process msg||
seq 751289| Thu May 03 15:21:57.551653 2018|Len: 78|GAP Ric IMRGN.OQ Flag2 12 Sym IMGGN-O msg SEQ_NO:2845 db:2843 diff 2 process msg||

But when I execute below nawk, it returns nothing.

Code:
System:xxxx% nawk  'match ($0, /(GAP)[^)]*\)\)/) {TMP1 = substr ($0, RSTART, RLENGTH-2); n = split (TMP1, T); TMP2 = sprintf ("%s %s\t\t%s", T[n-2], T[n-1], substr (T[n], 2)); if (!CNT[TMP2]++) print TMP2};' /tmp/File
System:xxxx%

I tired removing pattern to find "))" but again returns blank

Code:
 nawk  'match ($0, /(GAP)*/) {TMP1 = substr ($0, RSTART, RLENGTH-2); n = split (TMP1, T); TMP2 = sprintf ("%s %s\t\t%s", T[n-2], T[n-1], substr (T[n], 2)); if (!CNT[TMP2]++) print TMP2};' /tmp/File

# 9  
Old 05-03-2018
See post#2.
# 10  
Old 05-03-2018
What operating system are you using?

Exactly what command did you run that produced the diagnostics:
Code:
awk: syntax error near line 2
awk: illegal statement near line 2
awk: syntax error near line 5
awk: illegal statement near line 5

???
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 05-03-2018
Thank you Don, i need to use nawk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

2. Shell Programming and Scripting

Display Additional Variable string in awk print command

Hi all, I have script to monitor and sum up the total memory use up for each individual process. proc=$1 svmon -P -O summary=basic,unit=MB|awk 'NR>4'|grep -w "${proc}" |awk '{sum+=$3} END {printf "\t" sum """\n";}' But I would like the script to be able to display as following ... (3 Replies)
Discussion started by: ckwan
3 Replies

3. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

4. Shell Programming and Scripting

a cut-command or special format pattern in awk

Hi i read data with awk, 01.07.2012 00:10 227.72 247.50 1.227 1.727 17.273 01.07.2012 00:20 237.12 221.19 2.108 2.548 17.367 01.07.2012 00:30 230.38 230.34 3.216 3.755 17.412 01.07.2012 00:40 243.18 242.91 4.662 5.172 17.328 01.07.2012 00:50 245.58 245.41 5.179 5.721 17.128... (3 Replies)
Discussion started by: IMPe
3 Replies

5. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

6. Shell Programming and Scripting

awk command to find particular pattern in file.

Hi I am using the following command to look for anything other than "0000" in a comma seperated file on 11th field. Note: I am looking for "0000" including the double quotes. nawk -F"," '$11!='"0000"'{print $11}' file This is showing incorrect result. Help is appreciated (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

8. Shell Programming and Scripting

using command line arguments as columns for pattern matching using awk

Hi, I wish to use a column, as inputted by a user from command line, for pattern matching. awk file: { if($1 ~ /^8/) { print $0> "temp2.csv" } } something like this, but i want '$1' to be any column as selected by the user from command line. ... (1 Reply)
Discussion started by: invinclible0009
1 Replies

9. UNIX for Dummies Questions & Answers

Awk help needed for display particular field alone for searching pattern

Hi, I have a requirement for taking an particular number in a log file. if i grep for the particular string it will retrieve the entire line for the particular string. but i want to display only the string from each line which i am searching for, Note: The searching field varies its position... (3 Replies)
Discussion started by: senthilkumar_ak
3 Replies

10. UNIX for Advanced & Expert Users

how to display only the pattern

Hi, I have a file with 500 Lines and I want to search for a pattern within this file which starts with sm_ and ends with ). However I just want to print the pattern only and not the entire line. How do I do it ? Thanks, p (5 Replies)
Discussion started by: yerics
5 Replies
Login or Register to Ask a Question