- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting question: how to merge two file based on...
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
Discussions
Discussions
Forums
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
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
тАО03-17-2009 09:22 AM
тАО03-17-2009 09:22 AM
I have two file , and want to make it one , after comparing a common field:
file1
######
mm00 9900
nn00 8811
oo00 7722
pp00 663x
qq00 5544
rr00 4433
file2
##########
mm00 9:0:1
nn00 8:0:2
oo00 7:0:3
pp00 6:0:4
qq00 5:0:x
rr00 4:0:t
Output should be:
mm00 9900 9:0:1
nn00 8811 8:0:2
oo00 7722 7:0:3
pp00 663x 6:0:4
qq00 5544 5:0:x
rr00 4433 4:0:t
###################
Thanks in advance..
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2009 09:33 AM
тАО03-17-2009 09:33 AM
Re: scripting question: how to merge two file based on common strings?
# comm
...see the manpages.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2009 09:44 AM
тАО03-17-2009 09:44 AM
Solutioncat f2 |&
while read A B
do
read -p C D
if [[ ${A} != ${C} ]]
then
echo "ERROR: Files out of sync ($A != $C)"
else
echo "$A $B $D"
fi
done < f1
mm00 9900 9:0:1
nn00 8811 8:0:2
oo00 7722 7:0:3
pp00 663x 6:0:4
qq00 5544 5:0:x
rr00 4433 4:0:t
The comm(1) command does not do partial line pattern matching and is not really what you are looking for.
The above script assumes sort order and a 1 to 1 match for the lines. This can obviously be made much smarter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2009 10:03 AM
тАО03-17-2009 10:03 AM
Re: scripting question: how to merge two file based on common strings?
while read A B
do
Matches="$(grep -e "^${A}" f2)"
if [[ -z "${Matches}" ]]
then
echo "$A $B MISSING-DATA"
else
echo "${Matches}" |\
while read C D
do
echo "$A $B $D"
done
fi
done < f1
while read C D
do
grep -sqe "^${C}" f1
if [[ $? -ne 0 ]]
then
echo "$C MISSING-DATA $D"
fi
done < f2
gives the result (with edited files)
mm00 9900 9:0:1
nn00 8811 8:0:2
oo00 7722 7:0:3
pp00 663x 6:0:4
pp00 663x 6:5:3
pp00 663x 5:0:2
qq00 5544 5:0:x
rr00 4433 4:0:t
zz00 1234 MISSING-DATA
xx00 MISSING-DATA 3:1:7
Really just filling time before lunch... I should be doing many other things....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2009 11:18 AM
тАО03-17-2009 11:18 AM
Re: scripting question: how to merge two file based on common strings?
I'm sure JRF meant 'join', not comm'.
That should work, IF the files are sorted ALPHA. Watch out for free-format numbers.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2009 08:51 PM
тАО03-17-2009 08:51 PM
Re: scripting question: how to merge two file based on common strings?
here is the example
[root@rspc521 suraj]# cat 1
nn00 8811
oo00 7722
pp00 663x
qq00 5544
rr00 4433
[root@rspc521 suraj]# cat 2
mm00 9:0:1
nn00 8:0:2
oo00 7:0:3
pp00 6:0:4
qq00 5:0:x
rr00 4:0:t
[root@rspc521 suraj]# join 1 2
nn00 8811 8:0:2
oo00 7722 7:0:3
pp00 663x 6:0:4
qq00 5544 5:0:x
rr00 4433 4:0:t
Suraj