Script for getting the file merged


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for getting the file merged
# 1  
Old 07-31-2014
Wrench Script for getting the file merged

Hi,

I have two files separated by bars at each line

File 1 :
Code:
A|4356|13456|23456
A|4356|2986|98732
A|8765|218|1432567

File 2:
Code:
B|12|13456|1234567
B|11|13456|123456789
B|33|2986|98732
B|11|2986|14578965
B|8765|218|147584

Common field is third field like 13456, 2986 and 218 in both the files

Desired output after merge:
Code:
A|4356|13456|23456
B|12|13456|1234567
B|11|13456|123456789
A|4356|2986|98732
B|33|2986|98732
B|11|2986|14578965
A|8765|218|1432567
B|8765|218|147584

I wrote a code but Its not giving me the desired result.

Code:
rm /dir/new_file.txt
touch /dir/new_file.txt

for i in $(cat /dir/file1.txt);
do

 echo $i >> /dir/new_file.txt

 k=`echo $i |awk -F '|' '{print $3}'`
 for j in $(cat /dir/file2.txt);
 do
 w=`echo $j | awk -F '|' '{print "$3"}'`
 
 if [ "$w" == "$k" ]
 then
 echo $j >> /dir/new_file.txt
 fi
 
 done
done

Script is throwing error at red line, also I am not sure it give me the desired result or not.
Code:
testing_script_for_file.ksh[10]: 13456:  not found.

Please help me in this. Thanks in advance.


Moderator's Comments:
Mod Comment Please use CODE tags

Last edited by rbatte1; 07-31-2014 at 01:29 PM.. Reason: Added CODE tage
# 2  
Old 07-31-2014
I can't find an error with that line, works for me. Looks like the extraction of 13456 works fine, but then it tries to execute/open it. On your machine, not on mine.

How about
Code:
sort -t\| -k3,3nr file1 file2
A|4356|13456|23456
B|11|13456|123456789
B|12|13456|1234567
A|4356|2986|98732
B|11|2986|14578965
B|33|2986|98732
A|8765|218|1432567
B|8765|218|147584


Last edited by RudiC; 07-31-2014 at 01:45 PM..
# 3  
Old 07-31-2014
Thanks RudiC.

I want to do it using loop, could you please help me out in some other way because sorting is not successful every time as data is different each time.

On my machine my code is not working, please assist with some other code having the same functionality. I need it urgently.

Thank you.

Last edited by Mannu2525; 07-31-2014 at 05:38 PM.. Reason: Mistake
# 4  
Old 07-31-2014
RudiC gave you code that meets all of your stated requirements except that you don't like it. In what way does sort fail to meet your needs?

When we see statements like this after a seemingly good solution is proposed, it often means that the submitter is working on a homework assignment and is not allowed to use the tools proposed. Is this a homework assignment?

Are there other requirements that you haven't stated? If there are multiple lines for a given key in the first file, do you really want a copy of every line with that value in the second file duplicated in your new file for each occurrence of that value in the first file?

Is it important that your program run very slowly for medium sized files and run at a snail's pace for large files?

Is it important that your output file maintain the (possibly unsorted) order of lines in the 1st file and for all copies of lines from the 2nd file?

What OS are you using?

The people who read your posts and try to help you find solutions for your problems are volunteers. We are not on your payroll. This problem may be urgent for you, but you can't expect volunteers to ignore other things that they might want to do to satisfy your personal requirements. There are special forums that can be used for "Urgent" requests. In the regular forums requests that need to be responded to urgently are inappropriate. Is this another indication that you have a homework assignment that is due soon and you haven't figured out how to do it yet?
# 5  
Old 08-01-2014
Hi Don,

Nice to hear these words from you. This sorting idea is not working for me as there are 12-14 files that i am trying to merge and each in a specific order. Its not an assignment. Also its my first post here so i don't know the proper functioning of the forum.

Now my code is working fine for meSmilie. I got error due to my OS.

Thanks everyone.
# 6  
Old 08-01-2014
There is no magic here. You have a fairly large number of people who are intimately familiar with various aspects of various parts of Linux and UNIX systems. If you give these volunteers details about what you're trying to do, you can frequently get excellent tips on how to get an efficient solution to your problem. If you omit details about constraints on how the output is to be produced, we can all waste a lot of time wandering around paths that won't meet your needs.

Various systems provide various extensions to the standard utilities. If you always tell us what version of what OS you're using, we can avoid suggesting that you use extensions that are not available on your system.

Simply put, help us help you by giving us the information we would need to get the job done right on the system you will be using. Give us incomplete data, get one or more responses that meet all of your stated requirements, and then complain that it doesn't do something you never said was important in the first place, and the volunteers who wasted time trying to help you might not be interested in responding to your next request for help. This is just simple human nature.

Enough generalities...

If each of your input files are sorted in reverse numeric order on the key field as in your example (even if the key field is a different field in different files and has different field separators in different files), it would probably still be a lot faster to have an awk preprocessing step to create a single file with the key field, an added file number, and an added line number as fields from all of your input files; sort it by the key field, file #, and line #; and then use awk or sed to strip the file #, line #, and (if it had to be duplicated) the key fields added by the preprocessing step to just leave the desired sorted file as a result.

This could still be a lot faster than firing up a shell and awk for each line in each of the lines in each of all but the first of your 12 to 14 files multiplied by the number of lines in your 1st file (fork() and exec() are expensive operations).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

Multiple lines merged into one required

i have a file in the format acti_id|signature 1|abc def xyz 2|lmn pqr lmn 3|ggg ppp mmm it is in csv format i want the file in the format act_id|signature 1|abcdefxyz 2|lmnpqrlmn 3|gggpppmmm i have tried awk but without much success. i replaced the new line with null but it... (10 Replies)
Discussion started by: djrulz123
10 Replies

3. Linux

Dealing with a really high amount of reads merged

I'm trying to performance tune the I/O of my web server, which is at 41.1% reads merged (If my math is correct), which seems a tad high to just be going along with the defaults. Will modifying read_ahead_kb affect the value of "reads merged" in diskstats? If not, what's a good way of tracking... (2 Replies)
Discussion started by: thmnetwork
2 Replies

4. UNIX for Advanced & Expert Users

Sequence number merged with hypen, shell script

Hello Folks, i have to write shell scripting for given expected output manner. in given input we have to write shell script in such a way that sequence no can b merged/link between start and end digit with hyphen "-" symbol and rest of digit separated by "," Eg : For Input "2 6 7 8 11 12... (9 Replies)
Discussion started by: panchalh
9 Replies

5. Shell Programming and Scripting

Restricting the merged file size to 1 GB

I am working on a problem in which I need to merge 4 files (say f1,f2,f3 & f4 log files) & then prepare a final file. 1) If the final file created has size more than 1 GB then need to throw error (display error). 2) Need to check after every merge (say f1 + f2, f1 + f2 + f3) that whether... (2 Replies)
Discussion started by: nrm
2 Replies

6. Shell Programming and Scripting

Print merged rows from two files by applying if condition

Hi all, I have list of two kind of files and I want to compare the rows and print the merged data by applying if condition. First kind of file looks like: and second kind of file looks like : I want to print the rows present in second file followed by 3 more columns from first... (6 Replies)
Discussion started by: CAch
6 Replies

7. Shell Programming and Scripting

arrange merged data using sed

hi, i used paste file1.txt file2.txt > file3.txt to merge 2 columns from file1 and 4 columns from file2. file1 scaffold_217 scaffold_217 file2 CHRSM N scaffold_217.pf scaffold_217.fsa the result is as follows:- scaffold_217 scaffold_217 CHRSM ... (6 Replies)
Discussion started by: redse171
6 Replies

8. UNIX for Dummies Questions & Answers

merged 10 files with column extraction into one

Hi, I have 600 text files. In each txt file, I have 3 columns, e.g: File 1 a 0.21 0.003 b 0.34 0.004 c 0.72 0.002 File 2 a 0.25 0.0083 b 0.38 0.0047 c 0.79 0.00234 File 3 a 0.45 0.0063 b 0.88 0.0027 c 0.29 0.00204 ... my filename as "sc2408_0_5278.txt sc2408_0_5279.txt... (2 Replies)
Discussion started by: libenhelen
2 Replies

9. Shell Programming and Scripting

Sending output to a file - Merged from duplicate thread

Hi All, read dif echo `date +%Y%m%d`|./add $dif|./fmtdt %mon%dd The above script is for adding days to current date to find the new date. This script divides the current date into 20060220(YYYYMMDD) format and pass this output to add script. The add script will add the days to the... (2 Replies)
Discussion started by: muthu_nix
2 Replies

10. UNIX for Advanced & Expert Users

NFS server problems [merged]

I have a machine A NFS mounted on machine B I am doing a build from machine B on the MFS mounted dir of machine A but I keep getting the following: NFS server A not responding still trying. I go to machine A and can log onto machine A and everything seems fine. How do I go about finding... (6 Replies)
Discussion started by: brv
6 Replies
Login or Register to Ask a Question