- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Compare 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
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
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
02-26-2012 05:42 PM
02-26-2012 05:42 PM
Compare file
I have two files as below , I would like to compare the content
file 1
======
aaa.txt
111
222
bbb.txt
333
ccc.txt
ddd.txt
eee.txt
file 2
======
aaa.txt
444
bbb.txt
555
ccc.txt
ddd.txt
eee.txt
Result
======
aaa.txt
111
222
aaa.txt
444
bbb.txt
333
bbb.txt
555
ccc.txt
ccc.txt
ddd.txt
ddd.txt
eee.txt
eee.txt
Some content is the same ( eg. aaa.txt , bbb.txt , ccc.txt , ddd.txt , eee.txt ) , I would like to list its content by a condition to a file as the above result file - if the line is the same then output its next line(s) to the result file ,
can advise what can i do ?
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2012 09:18 PM
02-26-2012 09:18 PM
Re: Compare file
There is not really enough information for a good solution
0) Always just 2 input files, or more?
1) what pattern can be used to recognize 'some content is the same'
in the example: ".txt$". But what about for real? line starts with special character or word? NOT a number? Extra long, extra short?
2) is each match line garantueed to be present?
If not, How to sync back up on missing match line?
3) working with megabytes of data or gigabytes?
When working with megabytes all data can be kept in memory,
When working with gigabytes the solution with need to read the two files in near lock-step
Here is a solution using memory....
> variable k is the 'current key' based on matching the 'same content trigger'
> array x gets an entry per key, concatenating the current line and a new-line char for each line in all input
> array y gets an entry per unique key, ordered by number of arrival to keep printed in order of input.
> at the end for each entry in Y print the content of X, which is all lines for a given 'section'/key.
$ awk '/.txt$/{k=$0; if (!( k in x)) y[i++]=k } {x[k] = x[k] $0 "\n"} END{for (j=0;j<i;j++) print x[y[j]]} ' file1 file2
aaa.txt
111
222
aaa.txt
444
bbb.txt
333
bbb.txt
555
ccc.txt
ccc.txt
ddd.txt
ddd.txt
eee.txt
eee.txt
Good luck!
Hein.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2012 08:54 AM
02-27-2012 08:54 AM
Re: Compare file
>I would like to list its content by a condition to a file as the above result file -
It appears you are trying to merge the two files? Where the content in file 2 is added to the end of the same section in file 1?
Are the two files sorted by their *.txt entries?