- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Common lines between two files
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
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
04-15-2003 12:24 PM
04-15-2003 12:24 PM
Common lines between two files
I have two files with some common lines between them.
How Can I extract these common lines and put them into another file?
Are There some shell commands or scripts?
Thank You!!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2003 12:26 PM
04-15-2003 12:26 PM
Re: Common lines between two files
Thanks
Zafar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2003 12:30 PM
04-15-2003 12:30 PM
Re: Common lines between two files
when you are prompted with %,
enter l.
Then your matching lines will be in the output file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2003 12:30 PM
04-15-2003 12:30 PM
Re: Common lines between two files
See the man pages for 'comm' and 'diff'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2003 12:33 PM
04-15-2003 12:33 PM
Re: Common lines between two files
See the man pages for diff
Regards,
DR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2003 12:50 AM
04-16-2003 12:50 AM
Re: Common lines between two files
file1="file1"
file2="file2"
sort1="${file1}.sort"
sort2="${file2}.sort"
sort ${file1} > ${sort1}
sort ${file2} > ${sort2}
join ${sort1} ${sort2}
rm -f ${sort1} ${sort2}
hecou
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2003 01:18 AM
04-16-2003 01:18 AM
Re: Common lines between two files
If both files are sorted, 'comm' is *the* way to go:
# comm -12 file1 file2 >common
# comm -13 file1 file2 >onlyin2
# comm -23 file1 file2 >onlyin1
Then of course you have the question "are all lines unique?", and do you want unique lines squeezed? If lines are distributed unsorted and non-uniquely, and you want to see all lines:
# fgrep -f file1 file2
which has the disadvantage that if file1 contains short lines that might be occuring as part of files2's lines, you get too much. With GNU grep you can do better:
# fgrep -x -f file1 file2
And to squeeze the output, filter the dups:
# fgrep -x -f file1 file2 | uniq
Or if the sequence is not important:
# fgrep -x -f file1 file2 | sort -u
Or alternatively
# perl -e'$f2=pop;while(<>){$x{$_}++};@ARGV=($f2);while(<>){exists$x{$_}and print' file1 file2
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2003 02:46 AM
04-16-2003 02:46 AM
Re: Common lines between two files
you can use sdiff for this.
-o output
Use the next argument, output, as the name of a third file that is created as a user-controlled merging of file1 and file2. Identical lines of file1 and file2 are copied to output. Sets of differences, as produced by diff(1), are printed;
where a set of differences share a common gutter character. After printing each set of differences, sdiff prompts the user with a % and waits for user-typed commands.
If you give q, it will quit and the common lines will be present in file "output".
Regards
VJ