Discussion Locked

Merging of two files using unix utilitie

0 helpful
17 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have one static file with some tags in it. And I have one data file with the values and its position like page number, line number, column number.
No how do I merge these to files by using unix utilities.

17 replies

by vinnyd

You did not say what the resulting file should look like.
To combine two files together you can use the copy command.

cp sourcefile1+sourcefile2 destinationfile

That will put the two files together in sequencial order.

To merge them together like tags, page#, line#, column# you have to write a program to do it.

by tyswidan

IJust use the simple cat utility, for example if you have fil1 ( with tags)
and file2 ( with the data), all you have to do is:

cat file2 >> file1
done.

hope that helps.

by gordon

If the above answers your question, look no further, otherwise: Welcome to the world of hard-core unix: You need to refine your question. If the files are line-for-line ok and you just want to do a "column-merge" look at: paste -d If the tags in the first file refer to a line in the second file, then you may need to sort one (or both) files first-> get rid of duplicates (uniq) -> get rid of other stuff (cut, grep, sed) and then finally join (one of the harder commands to master). I had two files I had to merge together: One had IP addresses and printer names, the other had printer names and printer setups. I "linked" them together with the printer name, but had a lot of cleaning up to do first... Presevere, there is daylight after you master these. (My end file was 2900 bytes long, so it's a LOT smaller than any 'C' program)

by wcf

There are too many unknowns in your question for a definitive answer, but a general solution is to broaden your constraints slightly and use Perl, available on most systems. One line of code to open each file, a line or two of code to read the datafile into a hash table, and another line or 2 to scan the tag table gor tags and substitute from the hash table. This kind of problem is why Perl exists!

by a_rashad

you need to format file1 with a config. file2 ?
if so it is more likely to be done using a C program or a hard shell script
look for the following tools (for , cut , echo , head , typeset and sort).
main idea is that you will read from file2 theconfig file and place the values into variables such variables you will use to format file1 you have to sort values first then use echo to place the tags in the right places
use spaces and "\n" for positionsing after sorting the positions

by achuprinin

I's unclear from your question what kind of data represent your files. If the fist file contains format information for output data from the second one and files not binary.
Then you can use awk for making your output file. It allows using templates and may be run from command line without writing separate program file. And opposite right also. You can write program file that awk will use. The second approach is usefull when you going to make output file multiple times.

by rsati

actually, Ur question is not clear...
i assume that u have like this
file 1 has
page no column no line no
file 2 has
1 10 10
if want like this
file 3
page no 1 column no 10 line no 10

U can use cut and paste utility of unix andcan do that...

This discussion is closed to new replies.