awk command for complex search and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command for complex search and print
# 1  
Old 01-08-2014
awk command for complex search and print

Quote:
Hi,

i tired using awk command for getting the below output and it is not working.


Tried in the foums with awk with print next line but only if it predefined number rows but it will vary the number of rows in my case. the below command is not giving proper output for the value dbc10


awk '/dbc10/{print; getline; print; getline; print}'

Any direct command used in the awk to extract the output.
Code:
Input:


9999~cbc~100~209~ozzxczcz~10001001001~100~abc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
125~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
127~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~123
128~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345
9999~cbuc~101~109~oiuxczcz~1000198230001~1609~dbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
124~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
126~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~9999
1223~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345
9999~cbuc~9101~jdh2~bh89xczcz~100120001~309~kbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
125~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
127~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~123
128~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345

Code:
output:
 if i search for starting with 9999  and dbc10 then it should print from starting 9999 until next occurance of 9999. 

9999~cbuc~101~109~oiuxczcz~1000198230001~1609~dbc10
123~a~sdklfjl~sldoi~dbc10~~ksjdfnkjsdf~123456
124~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
126~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~9999
1223~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345

Thanks .[/QUOTE]
# 2  
Old 01-08-2014
How about:
Code:
awk '/^9999~/{p=0} /dbc10/{p=1}p' file

# 3  
Old 01-10-2014
Quote:
Using the below command i am not getting proper output.
Code:
awk '/^9999~/{p=0} /dbc10/{p=1}p' file

above command output
9999~cbuc~101~109~oiuxczcz~1000198230001~1609~dbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
124~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
126~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~9999
1223~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345
9999~cbuc~9101~jdh2~bh89xczcz~100120001~309~kbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
125~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
127~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~123
128~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345
Quote:
Actual output should in the below
9999~cbuc~101~109~oiuxczcz~1000198230001~1609~dbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
124~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
126~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~9999
1223~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345
# 4  
Old 01-10-2014
I cannot reproduce this with your input file.
Code:
$ awk '/^9999~/{p=0} /dbc10/{p=1}p' file
9999~cbuc~101~109~oiuxczcz~1000198230001~1609~dbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
124~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
126~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~9999
1223~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345

What is your OS and version?


--
Alternatively you could try:
Code:
awk '/^9999~/&&p{exit} /dbc10/{p=1}p' file


Last edited by Scrutinizer; 01-10-2014 at 02:27 PM..
# 5  
Old 01-10-2014
Code:
AIX OPERATING SYSTEM

# 6  
Old 01-10-2014
Also on AIX I cannot reproduce your outcome, with the sample you provided:
Code:
(AIX)$ awk '/^9999~/{p=0} /dbc10/{p=1}p' file
9999~cbuc~101~109~oiuxczcz~1000198230001~1609~dbc10
123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456
124~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345
126~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~9999
1223~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345

# 7  
Old 02-13-2014
After few modifications i am getting the proper output. thanks

Code:
awk '/^9999|~/{p=0} /dbc10/{p=1}p' file

but it is not returning any thing when i try to use as a parameter


input.txt
Code:
abc10
nnn12
llll12
34332
12o12po


Code:
for samp in `cat input.txt`
do
temp=`echo "T1"$samp`
awk '/^9999|~/{p=0} /$temp/{p=1}p' file
done

.

Last edited by Scrutinizer; 02-13-2014 at 03:40 PM.. Reason: Not code tags around everything. But around code and sample data
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

2. Shell Programming and Scripting

Search two patterns using awk to print the variable sum

Coins.txt: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (2 Replies)
Discussion started by: Ramesh M
2 Replies

3. Shell Programming and Scripting

echoing complex awk command into file fails

Using hp-ux's shell, I'm trying to echo a complex awk command into a script file for later use. But it fails on a newline character and splits the rest of the command onto the next line. echo ' printf("%s: TOTAL = %18.0lf\n", FILENAME, TOTAL) >> "TOTAL.TXT";' >>awk.script Looks... (3 Replies)
Discussion started by: Scottie1954
3 Replies

4. Shell Programming and Scripting

Help with sed/awk for reverse search and print

I have a file which is DFDG START DSFDS DSDS XXX END (VIO) AADD START SDSD FGFG END and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Discussion started by: pgbuddy
18 Replies

5. Shell Programming and Scripting

awk search and print

I have a script where I need to use awk, go through some output, which is stored in a variable, and find a string Xms and Xmx and print the results, including the rest of that string. Example of string: ... (3 Replies)
Discussion started by: cbo0485
3 Replies

6. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

7. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

8. UNIX for Dummies Questions & Answers

awk search and print .doc extensions

Hello, I'm trying to grab all lines in a file that match the extension using the following command: awk '/.doc/' Input.csv > WordFiles.dat This works, but if a line contains "doc" it still prints the line. I want to print only the lines with ".doc" Can someone show how to pick up only... (1 Reply)
Discussion started by: orahi001
1 Replies

9. Shell Programming and Scripting

awk how to print if the search pattern contains speace

the data file is as below: > cat master.cnf /usr| location for usr|5 /src/ver1| version 1 |10 /src/ver2/log| ver 2 log |25 /src/sys/apps/log| Application log for sys|36 /src/sys/apps/conf| configuration location for app|45 /src/sys/apps/bin| binary location app|55my script is as below: ... (1 Reply)
Discussion started by: McLan
1 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question