- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Filter records from Input File
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 08:05 AM
09-06-2006 08:05 AM
File 1
Record1: AAAERTD778 Q 100 DFGD 5676
Record2: AAAERTD558 Q 100 DFGD 323
Record3: AAAERTD448 Q 100 DFGD 778
Record4: AAAERTD178 Q 100 DFGD 5626
Record5: AAAERTD323 Q 100 DFGD 736
File 2
778
323
My requirement is to open File 2. Read line by line and search for the pattern in File 1.
That too patern should match only in first column.
For eg. If i take 778 from File 2 and do a general grep in File 1, it returns Records 1 and 3.But i need it to return only Record 1.
Problem is ,
1) If i just say grep for 778 and redirect output to another file it writes records 1 and 3 into new file.
2) If i cut first column and do the grep then
redirect output to another file it writes only the first column. But i need to write the whole record.
So i followed 2nd method and got the line nos using grep -n. Then again i looped based on physical line no and got the final output as what i needed. But i feel there should be a eay way to do it.
PLS HELP !! ONLY Shell script. NO PERL PLS.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 08:15 AM
09-06-2006 08:15 AM
Re: Filter records from Input File
AND
File 1 may not have same length in first column... it may be AAAERTD778 or
XYHAERT123778 or HAERT99978.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 08:16 AM
09-06-2006 08:16 AM
Re: Filter records from Input File
for NUMBER in $(cat file2)
do
grep AAAERTD${NUMBER} file1 > ${NUMBER}.output
done
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 08:19 AM
09-06-2006 08:19 AM
Re: Filter records from Input File
No Perl, too bad. OK, so create a read loop to read 'file2' and use its tokens as arguments to 'awk'. Something like:
...
while read TOKEN
do
awk -v TOKEN=${TOKEN} '$2~TOKEN {print}' file1
done < file2
Note that 'awk' counts fileds one-relative. Hence, $2 is awk's *first* whitespace-delimited field.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 08:28 AM
09-06-2006 08:28 AM
Re: Filter records from Input File
grep -E "[A-Z]*${NUMBER} " file1 > ${NUMBER}.output
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 09:07 AM
09-06-2006 09:07 AM
Re: Filter records from Input File
will this not pickup Record 1 and 3 if the search string is 778 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 01:53 PM
09-06-2006 01:53 PM
Re: Filter records from Input File
Easy... just have a first awk command generate the right awk program, then execute:
$ cat x.1
AAAERTD778 Q 100 DFGD 5676
AAAERTD558 Q 100 DFGD 323
AAAERTD448 Q 100 DFGD 778
AAAERTD178 Q 100 DFGD 5626
AAAERTD323 Q 100 DFGD 736
$ cat x.2
778
323
$ awk '{print "/^[A-Z]*" $1 "/"}' x.2 > /tmp/tmp_$$.awk
$ awk -f /tmp/tmp_$$.awk x.1
AAAERTD778 Q 100 DFGD 5676
AAAERTD323 Q 100 DFGD 736
$ rm /tmp/tmp_$$.awk x.1
For sake of completeness the helper looks like:
$ awk '{print "/^[A-Z]*" $1 " /"}' x.2
/^[A-Z]*778/
/^[A-Z]*323/
So it looks for lines starting with a bunch on characters from A to Z, followed by a specific number as found in the second file, followed by a space. If found, take the non-specified, default action, which is print the current line.
Hein
HvdH Performance Consulting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 05:59 PM
09-06-2006 05:59 PM
Re: Filter records from Input File
f1 contains...
AAAERTD778 Q 100 DFGD 5676
AAAERTD558 Q 100 DFGD 323
AAAERTD448 Q 100 DFGD 778
AAAERTD178 Q 100 DFGD 5626
AAAERTD323 Q 100 DFGD 736
f2 contains...
778
323
awk '{if($1~/^[A-Z]/){n=z[split($1,z,"[A-Z]")];x[n]=$0}for(i in x) if(i==$1)print x[i]}' f1 f2
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 06:28 PM
09-06-2006 06:28 PM
Re: Filter records from Input File
awk '{
if ($1~/^[A-Z]/) {
n = z[split($1,z,"[A-Z]")]
x[n] = $0
}
for (i in x)
if (i == $1)
print x[i]
}' f1 f2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2006 08:46 PM
09-06-2006 08:46 PM
Re: Filter records from Input File
Here's a short one-line ksh construct that does what you're looking for:
f1 contains...
AAAERTD778 Q 100 DFGD 5676
AAAERTD558 Q 100 DFGD 323
AAAERTD448 Q 100 DFGD 778
AAAERTD178 Q 100 DFGD 5626
AAAERTD323 Q 100 DFGD 736
f2 contains...
778
323
xargs
Record5: AAAERTD323 Q 100 DFGD 736
This look for key in file f2 there are in file f1 at the bgininng preceded by any chars and terminated by blank
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2006 03:00 AM
09-07-2006 03:00 AM
Re: Filter records from Input File
Sandman,
Your solution creates a large array with all of file1 does it not? And it will find only one match, even if there were 2 lines in file1 for a given file2 entry.
I would suggest to flip the files around:
awk "/^[0-9]*/{x[$1]=1} /^[A-Z]/ {n=z[split($1,z,\"[A-Z]\")]; if (x[n]) print}" 2.tmp 1.tmp
1) if the line starts wit a number, set a flag for that number in an array.
2) if the line starts with letter, split away a number part. If that number is flagged in the array, print the line.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2006 07:26 AM
09-14-2006 07:26 AM
Re: Filter records from Input File
anyways ... regarding this question.
I would like to reiterate the question one more time...
1) I want to open File 2, read line by line
and then search for that string in File 1's first column. I see many mentioning A-Z,0-9
etc... i dont want to stick on to that way...
whatever the start and end of column one be(even special characters, all i need is the search string should be in FIRST column of file 1. so may be it will be good to just cut the column one and search in it.
2) grep -E or grep -e not working for me
@lib>grep -e
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .
@lib>grep -E
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
Thanks for all your replies and patience.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2006 10:57 AM
09-14-2006 10:57 AM
Re: Filter records from Input File
while read i
do
while read j
do
str=$(echo $j | awk '{print z[split($1,z,"[A-Z]")]}')
if [[ $i = $str ]]; then
echo $j
fi
done < f1
done < f2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2006 11:07 AM
09-14-2006 11:07 AM
Re: Filter records from Input File
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2006 01:58 PM
09-14-2006 01:58 PM
SolutionI still kinda like the two-stage approach.
Short and sweet. Try this:
# cat x.1
AAAERTD778 Q 100 DFGD 5676
AAAERTD558 Q 100 DFGD 323
AAAERTD448 Q 100 DFGD 778
AAAERTD178 Q 100 DFGD 5626
AAAERTD323 Q 100 DFGD 736
# cat x.2
TD1
558
#
# awk '{print "$1 ~ /" $1 "/"}' x.2 > tmp.awk
# awk -f tmp.awk x.1
AAAERTD558 Q 100 DFGD 323
AAAERTD178 Q 100 DFGD 5626
For sake of completeness, the help file was:
# cat tmp.awk
$1 ~ /TD1/
$1 ~ /558/
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2006 01:14 PM
09-26-2006 01:14 PM
Re: Filter records from Input File
Your script working.. except final echo $j
removing all extra spaces within every column from particular record
if a column is 80 bytes and has values for
only 40 bytes ..rest will be blanks/spaces....but your code echos 40 bytes then one space and prints next columns.... basically length of whole record
is shrinked. PLS HELP !!!
while read i
do
while read j
do
str=$(echo $j | awk '{print z[split($1,z,"[A-Z]")]}')
if [[ $i = $str ]]; then
echo $j
fi
done < f1
done < f2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2006 03:37 PM
09-26-2006 03:37 PM
Re: Filter records from Input File
>if a column is 80 bytes and has values for only 40 bytes ..rest will be >blanks/spaces....but your code echos 40 bytes then one space and prints next >columns.... basically length of whole record is shrinked.
If you need to print the trailing spaces for every record then you need to set the internal field separator (IFS) to something other than a space. This way the echo $j command will capture and print everything, including the trailing spaces. See the script attached to this post:
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2006 04:16 PM
09-26-2006 04:16 PM
Re: Filter records from Input File
Did you try my last suggested solution?
It would retain spaces, and I suspect it will be relatively fast, which may be important for large data volumes.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2006 04:00 AM
09-27-2006 04:00 AM
Re: Filter records from Input File
Your code worked with that IFS=":" ... but for large data it just hung.
Hein,
Sorry i was concentrating on sandman's code.
YOUR CODE WORKED !! even for large data. cool.
Thanks to ALL