Conditional grep command to search entire file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional grep command to search entire file
# 1  
Old 09-06-2012
Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve.

1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic.

Log file structure:-

abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0 id=82c6a15ca06b2372c3b3ec2133fc8b14 referrer=google.com linkId=1ddoic

abc.com 20120829001416 127.0.0.1 dyn UD3BSAp8appncXlZ UD3BSAp8app4xHbz 0 0 N page Mozilla/5.0 id=82c6a15ca06b2372c3b3ec2133fc8b14 segments=

2. Now for the 1st log you can see I have invalid(none11111) 5th column. So I have to look for the actual 5th column value. 'id' column will help me to find that. So you have to run another grep based on the 'id' value so that you can find the actual 5th column in the same log file.
3. If you see the second log it has the exact matching 'id' value. So what I have to do I have to take the 5th column(UD3BSAp8appncXlZ) from the second log instead of the invalid one(none11111).
4. So basically I have to run another command which will do that. This is a conditional command which only runs when there is invalid 5th column.

Output:-

20120829001415, UD3BSAp8appncXlZ, linkId=1ddoic

Note:- I have bunch of log files where I have to perform the above procedure. But I have to come up with a single file as output after grepping through all the log files.
It has a format like abc-2012-10-01_00000,abc-2012-10-01_00001.... etc.

Thanks in advance.
# 2  
Old 09-06-2012
grep is not conditional. It matches lines. It's not a language...

awk is a language, and can do things conditionally. You can even tell it to read the same file twice... On the first pass, save the ID's. On the second pass, fill in missing ones...

Code:
awk 'NR==FNR && !($5 ~ /none/) {
        A[$12]=$5 # Save ID and key for later
        next # Skip to next line
}

($5 ~ /none/) && ($12 in A) { $5=A[$12] } 1' logfile logfile > output

# 3  
Old 09-06-2012
Hi Corona,

Thank you for your reply.
I am a newbie in unix. It would be very helpful if you explain your code little bit. I did not understand how do I run the loop through all the log files.

Thanks.
# 4  
Old 09-07-2012
awk is a language which loops over all lines by default.

So, for every single line, it runs both code blocks.

Code:
awk '# NR is the total line number.
# FNR is the line number in the file.
# When they are the same, it must be in the right file.
#
# $5 is the 5th field.  When it does not contain none,
# it is considered to be a valid line, and the 12th column
# and 5th column get saved for later in the array A.
# Then it skips to the next line without executing the block below.
NR==FNR && !($5 ~ /none/) {
        A[$12]=$5 # Save ID and key for later
        next # Skip to next line
}

# When the 5th column contains none, try to look
# it up in A.  If found, set the 5th column to its contents.
# The 1 at the end of the line tells it to print every line.
($5 ~ /none/) && ($12 in A) { $5=A[$12] } 1' logfile logfile

We give awk logfile twice, so it reads the same file twice. The first time it saves names for lookup. The second time, it tries to look up entries and substitute the data you wanted before printing every line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Search file pattern using grep command

I 'm writing a script to search particular strings from log files. The log file contains lines start with *. The file may contain many other lines start with *. I need to search a particular line from my log file. The grep command is working in command line , but when i run my script, Its printing... (7 Replies)
Discussion started by: vinus
7 Replies

3. UNIX for Dummies Questions & Answers

Bashrc File - Conditional Command Execution?

Hello All, I was wondering if there is a way to execute a command in my ".bashrc" file based on how I logged into the PC? I was thinking maybe there is a way to check how the user (*myself) logged in, maybe somehow with the who command along with something else, but I'm not sure... I know I... (7 Replies)
Discussion started by: mrm5102
7 Replies

4. Shell Programming and Scripting

Pattern search using grep command !

Hi, I am trying to do pattern search using grep command. But i donot know what mistake i'm doing. I am not getting the expected Result. could any one please help me out? $ cat b.ksh AasdjfhB 57834B 86234B 472346B I want to print the line which is starting with either A or 8 and... (10 Replies)
Discussion started by: nikesh29
10 Replies

5. UNIX for Advanced & Expert Users

search the entire man pages

Could someone please tell me how to search the entire man pages? I know man -k whatever searched the description but I wanna search the entire man page. (8 Replies)
Discussion started by: cokedude
8 Replies

6. UNIX for Advanced & Expert Users

Advance conditional grep command

Hi guys, I need your help to use advance grep command, In my example below, I have 5 container which has some information on it, and I need to grep specific word "PLMNCode=454F00" which will not only grep the line contains that word, but I need to print the output for the whole container. Input... (6 Replies)
Discussion started by: hapalon
6 Replies

7. UNIX for Dummies Questions & Answers

grep question: stop search from finding entire line

Sorry for the title, I really don't know how to word this question or what to even search for. I tried "grep one match", "grep 1 match", "stop grep" on both google and here and haven't found something that helps, so here I go: I have a file that's about 1.5 million lines long, every line looks... (3 Replies)
Discussion started by: rmoakler
3 Replies

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

9. UNIX for Dummies Questions & Answers

Search / Find / grep command ...

Is there any command, so I can retrieve all the records in a file from the first occurance of a search string, within that file? (4 Replies)
Discussion started by: videsh77
4 Replies

10. Shell Programming and Scripting

search in a directory using grep command

Hi, I want to search files/directories in /temp directory which starts with letter 'a'. How can I do that using grep command. I can find out like ls -ltr a* but I want to use grep command like ls -ltr | grep ..... like that. Please help. Malay (4 Replies)
Discussion started by: malaymaru
4 Replies
Login or Register to Ask a Question