01-07-2011
post #3: (by myself)
- Set field separator to pipe character i.e. |
- If current file read is "from_file", store each record ($0) in array a indexed as 1dt field in that record, and move to next line (This step will execute for all lines in from_file AND ONLY for from_file only)
- If current file read is "to_file", store each record ($0) in array b indexed as 1dt field in that record and move to next line (This step will execute for all lines in to_file AND ONLY for to_file only)
- Set field separator to tilde (~) character (By this time, both from_file and to_file are processed, and below steps are executed only for 3rd file i.e. key_file)
- If array a has non-null value at index $1 (1st field in key_file), set array a value as the 1st field (Override 1st field of this line with value in array a)
- If array b has non-null value at index $2 (2nd field in key_file), set array b value as the 2nd field (Override 2nd field of this line with value in array b)
- print lines in key_file (modified as per step 5 and 6) having output field separator as ~
Post #4: (By Scrutinizer) [If more than one file is passed to awk then, FNR gives line no in current file only, but NR gives current line from start (as if all files were in one file)]
- Set field separator to pipe character i.e. |
- Remove all trailing spaces from currrent line [Not done in earlier post]
- If current file read is "fromfile" (NR==FNR will be true only for 1st file passed to the command), store line in array A with 1st field as index (As was done eariler) and also increment m (So m will have line no processed so far)
- If current file read is "tofile", store line in array B with 1st field as index (As was done eariler). [NR-FNR==m will be true ONLY for 2nd file]
- Set field separator to tilde (~) character (It is set after 1st and 2nd file is processed)
- While processing lines in 3rd file, print array A value at index of 1st field and print array B value at index of 2nd field having output field separator as ~
Both posts are pretty much same, just implemented in different ways.
This User Gave Thanks to anurag.singh For This Post:
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi Friends,
Can any one help me with merging these file based on two columns :
File1:
A|123|99|SAMS
B|456|95|GEORGE
D|789|85|HOVARD
File2:
S|123|99|NANcY|6357
S|123|99|GREGRO|83748
A|456|95|HARRY|827|somers
S|456|95|ANTONY|546841|RUDOLPH|7263
B|456|95|SMITH|827|BOISE STATE|834... (3 Replies)
Discussion started by: sbasetty
3 Replies
2. Shell Programming and Scripting
Hi,
I have file1.txt and file2.txt and would like to create file3.txt based on one column in UNIX
Eg:
file1.txt
17328756,0000786623.pdf,0000786623
20115537,0000793892.pdf,0000793892
file2.txt
12521_74_4.zip,0000786623.pdf
12521_15_5.zip,0000793892.pdf
Desired Output
... (5 Replies)
Discussion started by: techmoris
5 Replies
3. Shell Programming and Scripting
Hi All,
I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations:
1. I am restrained to 2 input files only.
2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies
4. Shell Programming and Scripting
Hi Guys,
After windows died on my netbook I installed Lubuntu and discovered Gawk about a month ago. After using Excel for 10+ years I'm amazed how quick and easily Gawk can process data but I'm stuck with a little problem merging data from multiple lines.
I'm an SEO Consultant and provide... (9 Replies)
Discussion started by: Jamesfirst
9 Replies
5. Shell Programming and Scripting
I've been a Unix admin for nearly 30 years and never learned AWK. I've seen several similar posts here, but haven't been able to adapt the answers to my situation. AWK is so damn cryptic! ;)
I have a single file with ~900 lines (CSV list). Each line starts with an ID, but with different stuff... (6 Replies)
Discussion started by: protosd
6 Replies
6. Shell Programming and Scripting
Hi Friends,
I have a file
file1.txt
1|ABC|3|jul|dhj
2|NHU|4|kil|eu
3|hjd|34|hfd|43
file2.txt
1||3|KING|dhj
2|NHU||k|
3|hjd|34|hd|43
i want to merge file1.txt file2.txt based on the column null values in file2.txif there are any nulls in column values , (5 Replies)
Discussion started by: i150371485
5 Replies
7. Shell Programming and Scripting
011111123444 1234 1 20000
011111123444 1235 1 30000
011111123446 1234 3 40000
011111123447 1234 4 50000
011111123448 1234 3 50000
File2:
011111123444,Rsttponrfgtrgtrkrfrgtrgrer
011111123446,Rsttponrfgtrgtr
011111123447,Rsttponrfgtrguii
011111123448,Rsttponrfgtrgtjiiu
I have 2 files... (4 Replies)
Discussion started by: vinus
4 Replies
8. UNIX for Dummies Questions & Answers
Hi, I am trying to selectively merge two files based on keys reported in the 1st column.
File1:
#file1-header1
file1-header2
111 qwe rtz uio
198 asd fgh jkl
165 yxc
789 poi uzt rew
89 lkj
File2:
#file2-header2
file2-header2
165 ghz nko2 ... (2 Replies)
Discussion started by: dovah
2 Replies
9. Shell Programming and Scripting
Hi,
I am struggling with the an awk command to merge two files based on a common key.
I want to append the value from File2 ($2) onto the end of File1 where $1 from each file matches - If no match then nothing is apended
File1
COL1|COL2|COL3|COL4|COL5|COL6|COL7... (3 Replies)
Discussion started by: Ads89
3 Replies
10. Shell Programming and Scripting
Join and merge multiple files with duplicate key and fill void columns
Hi guys,
I have many files that I want to merge:
file1.csv:
1|abc
1|def
2|ghi
2|jkl
3|mno
3|pqr
file2.csv: (5 Replies)
Discussion started by: yjacknewton
5 Replies
LEARN ABOUT OSX
svk::command::merge
SVK::Command::Merge(3) User Contributed Perl Documentation SVK::Command::Merge(3)
NAME
SVK::Command::Merge - Apply differences between two sources
SYNOPSIS
merge -r N:M DEPOTPATH [PATH]
merge -r N:M DEPOTPATH1 DEPOTPATH2
merge -r N:M [--to|--from] [PATH]
OPTIONS
-r [--revision] N:M : act on revisions between N and M
-c [--change] N : act on change N (between revisions N-1 and N)
using -N reverses the changes made in revision N
-I [--incremental] : apply each change individually
-a [--auto] : merge from the previous merge point
-l [--log] : use logs of merged revisions as commit message
-s [--sync] : synchronize mirrored sources before operation
-t [--to] : merge to the specified path
-f [--from] : merge from the specified path
--summary : display related logs in this merge
--verbatim : verbatim merge log without indents and header
--no-ticket : do not record this merge point
--track-rename : track changes made to renamed node
-m [--message] MESSAGE : specify commit message MESSAGE
-F [--file] FILENAME : read commit message from FILENAME
--template : use the specified message as the template to edit
--encoding ENC : treat -m/-F value as being in charset encoding ENC
-P [--patch] NAME : instead of commit, save this change as a patch
-S [--sign] : sign this change
-C [--check-only] : try operation but make no changes
--direct : commit directly even if the path is mirrored
perl v5.10.0 2008-08-04 SVK::Command::Merge(3)