line-by-line matching script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting line-by-line matching script
# 1  
Old 08-05-2008
line-by-line matching script

First post by a newbie. Pls help....

#!/bin/ksh
OBJECT_LIST=/export/home/russellc/infile99.lst
OUTPUT_FILE=/export/home/russellc/outfile99.lst
cd /export/home/russellc/
for mystuff_obj in `cat $OBJECT_LIST`
do
find . -name "scanfile99.txt" | xargs grep -in $mystuff_obj >> $OUTPUT_FILE
done


Contents of /export/home/russellc/infile99.lst
--------------------------------------------
ABCD
EFGH
IJKL
MNOP

Contents of /export/home/russellc/scanfile99.txt
----------------------------------------------
I have ABCD in my mind
She thinks I think WXYZ
Everybody thinks I think EFGH
Nobody thinks I think MNOP


Contents of /export/home/russellc/outfile99.lst
---------------------------------------------
1:I have ABCD in my mind
3:Everybody thinks I think EFGH
4:Nobody thinks I think MNOP

In short, the script result (outfile99.lst) shows the matched lines from scanfile99.txt.

How can I make the script output the matched lines from infile99.lst instead?
Ex.,
ABCD
EFGH
MNOP


Thanks in advance!
# 2  
Old 08-05-2008
With grep:

Code:
grep -n -f "$OBJECT_LIST" scanfile99.txt

Regards
# 3  
Old 08-05-2008
Code:
NR==FNR {a[$0]; next;}
{for (arri in a)
{
        if(match($0,arri))
        {
                print $0
                next
        }
}
}

# 4  
Old 08-06-2008
Hi,

Check the following script,

for i in `cat file1`
do
sed -n ' '/${i}/'p' file2
done

Last edited by thangaraju; 08-06-2008 at 09:32 AM..
# 5  
Old 08-06-2008
for reading the contents of a file, using a while "fed from below" is way better (faster and less resources)
Code:
while read line
do
   echo "do somethings here"
done < /apth/to/my/file

while is the loop reserved word.
read is the program/built in read function
and line is the variable that holds the line (you can use anything there, is juts a var)
Code:
while read teletubie
do
   echo "$teletubie is a horrible actor"
done < /apth/to/niptuk/cast

also, when im doing some scripting, i always have this open (i even carry a tar.gz of those with me so i can erad them offline)
AWK Language Programming - Table of Contents
Advanced Bash-Scripting Guide
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

6. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

7. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

8. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

9. Shell Programming and Scripting

sed script - transform 1st matching line only

How do transform only the first line that matches my regex? I've tried q but it just quit my script in the middle. (9 Replies)
Discussion started by: yogert909
9 Replies

10. UNIX for Dummies Questions & Answers

how to delete line with matching text and line immediately after

hello experts, I have a file: File1 Sample Test1 This is a Test Sample Test2 Another Test Final Test3 A final Test I can use sed to delete the line with specific text ie: sed '/Test2/d' File1.txt > File2.txt How can I delete the line with the matching text and the line immediately... (6 Replies)
Discussion started by: orahi001
6 Replies
Login or Register to Ask a Question