grep based on pattern in a line and print the column before that


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep based on pattern in a line and print the column before that
# 1  
Old 06-21-2012
Java grep based on pattern in a line and print the column before that

Code:
$ cat file.log
Message Number = [ ]: Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length [472]

I have the above line in the file.log

My requirement is to find in which column the reqUUID110612105016$BT is present(column delimiter is | ) and i need to find the column number of DummyAcctNo field and display the value in that column.

The clue here is there is 12 columns inbetween reqUUID110612105016$BT and DummyAcctNo column with delimiter as |

In simple words, The only input i know is reqUUID110612105016$BT and i need to display the output as DummyAcctNo.

Pl help. I have no idea what to do.
# 2  
Old 06-21-2012
Code:
awk -F'|' '{for(i=1;i<=NF;i++){ if($i == "reqUUID110612105016$BT") { print $(i-13) } }}' file.log

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-21-2012
Quote:
Originally Posted by balajesuri
Code:
awk -F'|' '{for(i=1;i<=NF;i++){ if($i == "reqUUID110612105016$BT") { print $(i-13) } }}' file.log


Thanks for the script. Its working fine. I need to implement small modification in the script, but it's not working Smilie

I need to pass reqUUID110612105016 alone as a variable to the above script. I have tried the below script.

Code:
$sh
$x=reqUUID110612105016
$ cat File.log|awk -F'|' '{for(i=1;i<=NF;i++){ if($i == "'$x'$BT") { print $(i-13) } }}'

But its not working Smilie

Pl help me in implementing this.
# 4  
Old 06-21-2012
Hi, try:
Code:
awk -F'|' '{for(i=1;i<=NF;i++) if($i == x "$BT") print $(i-13)}' x=reqUUID110612105016

or
Code:
var=reqUUID110612105016
awk -F'|' '{for(i=1;i<=NF;i++) if($i == x "$BT") print $(i-13)}' x="$var"

# 5  
Old 06-21-2012
MySQL Thanks a lot.

Quote:
Originally Posted by Scrutinizer
Hi, try:
Code:
awk -F'|' '{for(i=1;i<=NF;i++) if($i == x "$BT") print $(i-13)}' x=reqUUID110612105016

or
Code:
var=reqUUID110612105016
awk -F'|' '{for(i=1;i<=NF;i++) if($i == x "$BT") print $(i-13)}' x="$var"


Thanks a lot Scrutinizer. The code works like charm for my requirement Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

3. Shell Programming and Scripting

Print column based on pattern

Hi all, how print on columns when contain un pattern specific, e.g. $cat file1 3234 234 2323 number1 number2 number3 123 242 124 124 number2 324 424 543 626 number1 3463 234 534 345 number3 6756 345 2352 334 345 234 need output file1 way (2 Replies)
Discussion started by: aav1307
2 Replies

4. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

7. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

8. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies

9. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies
Login or Register to Ask a Question