The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find number of columns in a file rahul26 UNIX for Dummies Questions & Answers 13 05-08-2009 05:11 AM
How to find number of lines in a file somesh_p Shell Programming and Scripting 2 12-19-2007 09:15 PM
How to grep a number in a file name ahjiefreak Shell Programming and Scripting 3 12-11-2007 04:56 AM
find the highest number in the file systemali Shell Programming and Scripting 30 03-23-2006 01:31 PM
how to use the grep to find detail in a file etravels SUN Solaris 1 03-10-2004 02:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-11-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to grep a number in a file to find them in another file

Hi,

I tried to simulate some example in Shell particularly finding item and matching in another file.

A very simple example:-
Basically I have 2 files, file A and file B.

In file A, I have columns of fields such that:-

aaa 107
bbb 108
ccc 109

In file B, I have columns of fields such that:-
101 2 1
102 3 1
107 2 1
108 3 1
109 2 1

I would like to know, if I would like to extract let say first element of file A and compare with file B elements.

If found, I would like to have the position of element 107 in file B in this case it is on 3rd line. From the elements found in file B, I would like to perform some computation on their fields. And the same goes for other elements in file A which in the end will pipe to another output file.

Currently, I tried to do below:-

#!/bin/sh

cat a.txt | awk '
{

for $2 in a.txt

do

grep -i $2 b.txt
# printf("%d", ); #to print the position of the string found in b.txt
}'

cat b.txt| awk '
{
count[$i]=$2+$3;

}
END{
printf("%d",count[$i]);
}'
done

I could not get it working because of several reason:-

i)rom the grep option -i it match any numbers entries start with let say "1"
rather than the exact number.

ii)I could not figure out how to open two files similarly using AWK so I can extract fields.

Please advise. Thanks.


-Jason
  #2 (permalink)  
Old 12-11-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

Some of the initial complexity can be removed by using command join:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate a join, using a field different from 1.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash my-nl join

echo
echo " Input files:"
cat data1
echo
cat data2

echo
echo " Result of join:"
join -1 2 data1 data2

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
my-nl (local) 296
join (coreutils) 5.2.1

 Input files:
aaa 107
bbb 108
ccc 109

101 2 1
102 3 1
107 2 1
108 3 1
109 2 1

 Result of join:
107 aaa 2 1
108 bbb 3 1
109 ccc 2 1
You may still wish to use awk for the post-join processing, because that makes it easy to use the resulting fields. See man pages for details ... cheers, drl
  #3 (permalink)  
Old 12-11-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi,

Thanks for the below input.

However if we are using Join command, the position of the element in the matching file could not be retrived as similar elements are already grouped together.

I assume that the join results would be pipe into a new output file.

So far I have:-

cat a.txt| while read LINE
do

char=`echo "${LINE}"| awk '{print $2}'`

echo $char

cat b.txt| awk '{

#grep -n $char b.txt| tr ":" " "|awk '{print $1}'

done

However, I am stuck in how to retrieve the first field of b.txt.
Next, I am stuck in getting the current line found in b.txt

Please advise.Thanks.


-Jason
  #4 (permalink)  
Old 12-11-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi, Jason.

That is why I asked you to provide the desired results -- to let us see what you need. An example is better than 1000 words, yes?
Code:
#!/usr/bin/env sh

# @(#) s2       Demonstrate a join, with file position added.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash my-nl join

echo
echo " Input files:"
cat data1
echo
cat data2
echo
cat -n data2 >data3
cat data3

echo
echo " Result of join:"
join -1 2 -2 2 data1 data3

exit 0
Code:
% ./s2

(Versions displayed with local utility "version")
GNU bash 2.05b.0
my-nl (local) 296
join (coreutils) 5.2.1

 Input files:
aaa 107
bbb 108
ccc 109

101 2 1
102 3 1
107 2 1
108 3 1
109 2 1

     1  101 2 1
     2  102 3 1
     3  107 2 1
     4  108 3 1
     5  109 2 1

 Result of join:
107 aaa 3 2 1
108 bbb 4 3 1
109 ccc 5 2 1
cheers, drl
  #5 (permalink)  
Old 12-11-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi,

I miss ed out the output in earlier post.

Currently, my input file of A.txt

aaa 1
bbb 2
ccc 3
ddd 100

B.txt

2 3 4
3 4 5

My code so far is:-

#!/bin/bash

cat a.txt| while read LINE
do

#to get the second field of a.txt
char=`echo "${LINE}"| awk '{print $2}'`

echo $char

#to grep the line position of b.txt which match the element in a.txt
grep -n $char b.txt | tr ":" " "| awk '{print $1}'

done

The output is:-
1

2
1

3
1
2

100

The problem here is i) grep does not want to recoqnize only the first field of b.txt (which you can see for 3 in a.txt there is 1, 2 returned because there is "3" in b.txt for two entries.

ii)And there is problem if I applied grep -m 1 $char b.txt|awk '{print $1}'
for printing the matching element for first field of b.txt. The output would be

1

2
2

3
2

100


Ultimately, what I would like to have the desired otuput is:-

1 Not Found
2 Position 1 3+4
3 Position 2 4+5
100 Not Found


Thanks.
-Jason
  #6 (permalink)  
Old 12-11-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

I am still using your original 2 data sets, which I call data1 and data2. The final output consists of a first set of matching lines, with all the fields from both files. The second set is the set of lines from data2 that is unmatched with the text string " Not found" appended.

The line position is given, as you can see, because the file was augmented with the line numbers by "cat -n".

The output from the first join should be easily read by awk, and the fields will be available to you, as usual, in variables "$1", "$2", etc.

If you need the reports to be different, you can change the join options -- see man join.
Code:
#!/usr/bin/env sh

# @(#) s3       Demonstrate a join, file position, unmatched lines.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash my-nl join

echo
echo " Input files:"
cat data1
echo
cat data2
echo
cat -n data2 >data3
cat data3

echo
echo " Result of join for matched lines:"
join -j 2 data1 data3

echo
echo " Result of join unmatchable lines from data2"
join -v 2 -j 2 data1 data3 |
sed -e 's/$/ Not found/'

exit 0
Producing:
Code:
% ./s3

(Versions displayed with local utility "version")
GNU bash 2.05b.0
my-nl (local) 296
join (coreutils) 5.2.1

 Input files:
aaa 107
bbb 108
ccc 109

101 2 1
102 3 1
107 2 1
108 3 1
109 2 1

     1  101 2 1
     2  102 3 1
     3  107 2 1
     4  108 3 1
     5  109 2 1

 Result of join for matched lines:
107 aaa 3 2 1
108 bbb 4 3 1
109 ccc 5 2 1

 Result of join unmatchable lines from data2
101 1 2 1 Not found
102 2 3 1 Not found
Best wishes ... cheers, drl
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0