Using grep with multiple loops in reading files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep with multiple loops in reading files
# 1  
Old 07-11-2012
Using grep with multiple loops in reading files

I am trying to read a file line by line and then search that line in another file and get a particular column from the second file.
I have written this code.but its not working properly
Code:
#!/bin/sh
while read keyword in duplicate.txt 
do 
  echo $keyword 
  while read line
	  do
                    grep -w keyword | cut -d"|" -f8 
          done <Sri1.log >> f.txt 
done

duplicate.txt is the first file.It has:-

Code:
o3EhNzKqTwIhcMxg101209090000024_1
IUxBNzKlZfghcMxf101209090000024_1

and Sri1.log is the second file in which i want to search these two values and get the column.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-11-2012 at 02:35 AM.. Reason: code tags, see PM!
# 2  
Old 07-11-2012
The syntax while read ... in ... does not exist. You are already feeding the read with < Sri1.log.
On the other hand, you do not feed the grep -w with StdIn or a file.

Try something like:
Code:
grep -wf duplicate.txt Sr1.log > f.txt


Last edited by zaxxon; 07-11-2012 at 02:55 AM.. Reason: change -fw to -wf
# 3  
Old 07-11-2012
Thanx for quick reply but
grep -fw gives an error

Code:
grep: w: No such file or directory

# 4  
Old 07-11-2012
Oops
Code:
grep -wf ...

Please use code tags. You got a PM about it and I put a moderator note with a link in it into your first post.
# 5  
Old 07-11-2012
I tried this also but its not working Smilie
it doesn't give error but the output file is empty

---------- Post updated at 03:58 AM ---------- Previous update was at 02:07 AM ----------

I have tried running this code since i need 8th column from my file(Sri1.log) but only for those lines which have value that is in "duplicate.txt"

Code:
 
grep -wf duplicate.txt | cut -d"|" -f8 Sri1.log >>f.txt

But this code
returns all the values of 8th column of complete file as it is not using the output given by grep command.
# 6  
Old 07-11-2012
This code is wrong:
Code:
grep -wf duplicate.txt | ...

-f expects a file as a list of patterns to match. So in this case you miss the 2nd file which should be searched.

To shorten this, please post some lines of the input and the expected output using code tags, thanks.
# 7  
Old 07-11-2012
I have two files duplicate.txt and Sri1.log

Code:
 
duplicate.txt has
o3EhNzKqTwIhcMxg101209090000024_1
IUxBNzKlZfghcMxf101209090000024_1

Code:
 
Sri1.log has
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   |  Payment | Exchange ...  | Exchange sent   ....   | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | xengine......           | {previous....., Split, exchange-properties={ClaimSystemID=MS,......, RemittanceId=IUxBNzKlZfghcMxf101209090000024_1, .....}

2012-05-09 10:34:51.920 | SYNC  | 00052 | DEBUG   | EUO_Main Remittance Flow | Exchange        | Exchange ..... | AF4B93E6-7DFF-49E6-8AA3-065AA09A01C7-0000000  | service.adapter:UCF Version Creator | {previous-component...., RemitProcessingID=014BB9C7-B83E-48FC-9745-6A8A45F8F9AD, RemittanceId=o3EhNzKqTwIhcMxg101209090000024_1,......}
2012-05-09 10:28:47.942 | SYNC  | 00040 | INFO    | Customer_Receive Payment | Task            | Task started                           | B678C56D-96DA-4FFC-B40E-9A032A2EB12E          | filesystem:CERR                     | {}

Code:
 
Output Required
 B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003
AF4B93E6-7DFF-49E6-8AA3-065AA09A01C7-0000000

I want value only if line has the text searched from the first file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

2. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

3. Shell Programming and Scripting

reading information from a table and apply a command on multiple files

Hey gyuz, I wanna calculate the number of mapped reads of a bam file in a region of interest. I used this code to do so : samtools view input.bam chrname:region1 > region1.txt This will store all the reads from given bam file within the region of interest in region1.txt Now I have... (5 Replies)
Discussion started by: @man
5 Replies

4. UNIX for Dummies Questions & Answers

Reading and writing data to and from multiple files

Hi, I have several text files. One main file contains the detail data, other have some information to extract data from the main file, and some are empty files. Examples are shown below: The main file look like: MainFile.txt >Header1 data1...data1... >Header2 data2...data2... ... ...... (2 Replies)
Discussion started by: Fahmida
2 Replies

5. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

6. UNIX for Dummies Questions & Answers

Reading compressed files during a grep search

All, The bottom line is that im reading a file, storing it as variables, recursively grep searching it, and then piping it to allow word counts as well. I am unsure on how to open any .zip .tar and .gzip, search for keywords and return results. Any help would be much appreciated! Thanks (6 Replies)
Discussion started by: ryan.lee
6 Replies

7. Shell Programming and Scripting

file reading in nested loops

I have to to read files simultaneously in two nested loops,but am getting error can anyone do the needful. useridFile=userIds.txt fname=kiran.txt exec<$useridFile while read line do echo "User IDs are..$line" USER_ID=$line REMOTE_DIR_LOCATION="/home/test/$USER_ID" SOURCE_DIR=$USER_ID... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

8. Shell Programming and Scripting

reading multiple files

Hello I have a requirement where i have to loop through certain directory which will have multiple files. After looping through i have to make a list of file names exisitng in the directory and write them to a sequental file and each file should be delimited by ^%^ i.e If i have 3 files A,... (3 Replies)
Discussion started by: dsdev_123
3 Replies

9. Shell Programming and Scripting

Reading files using grep/sed/awk

After pouring over my LTKS and Unix in a nutshell, I'm stuck! I have a large (BMC Report File) that has breaks on DM (district managers). After a report header, there is a DM header like: DM: DBP AARON ROBERTS At the end of each DM break, there is: ** END OF REPORT ** ... (6 Replies)
Discussion started by: Jodyman
6 Replies

10. Shell Programming and Scripting

Reading multiple tags from .asx files

Hi, I'm trying to wget a series of .asx files, read <title> and <ref> tags from them and then sending that information to wget. asx files contains the following information <ASX VERSION="3.0"> <ENTRY> <STARTTIME Value="00:00:00.0" /> <TITLE>title of the movie</TITLE> <REF... (0 Replies)
Discussion started by: underlig
0 Replies
Login or Register to Ask a Question