Sponsored Content
Top Forums Shell Programming and Scripting awk command to display particular pattern Post 303016138 by Don Cragun on Friday 20th of April 2018 10:34:21 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 05:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy