awk command to display particular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to display particular pattern
# 1  
Old 04-19-2018
awk command to display particular pattern

Hi
I am using awk to print 10,11 column but it is not displaying required output.
Please let me know how I can browse through the line and extract the required one

Example: I have below 2 lines in file
Code:
seq 49960| Thu Apr 19 10:57:40.726182 2018|Len: 89|GAP for CL/U18 9P-NC (CL90U8)) gap cnt=7337.  msg csn=8815 (16 bit), pf csn=1477 (16 bit)||

seq 49961| Thu Apr 19 10:57:40.727959 2018|Len: 93|DUP seq for CL/X18 8.5P-NC (CL85W8)) n_msgs=2150.  msg csn=863 (16 bit), pf csn=3012 (16 bit)||

I use

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}

I want after awk
I should get in

Code:
1st column                      2nd column
CL/U18 9P-NC                 CL90U8
CL/X18 8.5P-NC              CL85W8

# 2  
Old 04-19-2018
This is difficult if not impossible, as you have spaces as field separators but also contained in desired output values. Do you see a chance to define fields and separators differently?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-19-2018
Well, this might work, taking advantage of the data structure as shown:
Code:
awk  '
match ($0, /(GAP|DUP)[^)]*\)\)/)        {TMP1 = substr ($0, RSTART, RLENGTH-2)
                                         n = split (TMP1, T)
                                         TMP2 = sprintf ("%s %s\t%s", T[n-2], T[n-1], substr (T[n], 2))
                                         if (!CNT[TMP2]++) print TMP2
                                        }
' file
CL/U18 9P-NC	CL90U8
CL/X18 8.5P-NC	CL85W8

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-19-2018
Quote:
Originally Posted by RudiC
This is difficult if not impossible, as you have spaces as field separators but also contained in desired output values. Do you see a chance to define fields and separators differently?

I tried below, but not able to remove one "(" Smilie

Code:
cat OutFile | grep GAP | awk -F'|' '{print $4}' |awk -F')' '{print $1}' |awk -F' ' '{print $3,$4,$5}'

Output

Code:
CL/U18 16P-NC (CL160U8
CL/N18 5.5P-NC (CL55S8
CL/Q18 9.5P-NC (CL95T8
CL/Q18 6.5P-NC (CL65T8
CL/M18 5.5P-NC (CL55R8

---------- Post updated at 11:13 AM ---------- Previous update was at 11:07 AM ----------

Quote:
Originally Posted by RudiC
Well, this might work, taking advantage of the data structure as shown:
Code:
awk  '
match ($0, /(GAP|DUP)[^)]*\)\)/)        {TMP1 = substr ($0, RSTART, RLENGTH-2)
                                         n = split (TMP1, T)
                                         TMP2 = sprintf ("%s %s\t%s", T[n-2], T[n-1], substr (T[n], 2))
                                         if (!CNT[TMP2]++) print TMP2
                                        }
' file
CL/U18 9P-NC	CL90U8
CL/X18 8.5P-NC	CL85W8



I am getting the below error
Code:
awk  '
>> match ($0, /(GAP|DUP)[^)]*\)\)/)        {TMP1 = substr ($0, RSTART, RLENGTH-2)
>>                                          n = split (TMP1, T)
>>                                          TMP2 = sprintf ("%s %s\t%s", T[n-2], T[n-1], substr (T[n], 2))
>>                                          if (!CNT[TMP2]++) print TMP2
>>                                         }
>> ' FILE
awk: syntax error near line 2
awk: bailing out near line 2

# 5  
Old 04-19-2018
Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-19-2018
Thanks RudiC, that worked with nawk.
# 7  
Old 04-20-2018
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.
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