Merge files and copy some data using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge files and copy some data using sed or awk
# 8  
Old 09-22-2014
Hello kenshinhimura,

Seems this given data is a bit different from previously provided input. Following may help you.

Code:
awk 'NR==FNR{if($0 ~ /Name/){$1="";e=$0;getline;A[$4]=$3 OFS $1 OFS e;}} ($1 in A){print $2 OFS $1 OFS A[$1]}' file1 file12

Output will be as follows.
Code:
server1.com 192.216.7.80 i-21c95a6e9 us-west-2c  wow Master Device
server2.com 192.216.3.20 i-21690a303 us-west-2b  data W3

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 09-22-2014
Singh its working, the explanation you gave on previous page is the same on this one?
thanks

can you please explain it in english once again. the codes..please????

---------- Post updated at 01:33 PM ---------- Previous update was at 01:31 PM ----------

Your last replay, codes below are more dynamic for me. it can change the value. hop eyou can explain it.

Code:
 awk 'NR==FNR{if($0 ~ /Name/){$1="";e=$0;getline;A[$4]=$3 OFS $1 OFS e;}} ($1 in A){print $2 OFS $1 OFS A[$1]}'

# 10  
Old 09-22-2014
Quote:
Originally Posted by kenshinhimura
Singh its working, the explanation you gave on previous page is the same on this one?
thanks

can you please explain it in english once again. the codes..please????

---------- Post updated at 01:33 PM ---------- Previous update was at 01:31 PM ----------

Your last replay, codes below are more dynamic for me. it can change the value. hop eyou can explain it.

Code:
 awk 'NR==FNR{if($0 ~ /Name/){$1="";e=$0;getline;A[$4]=$3 OFS $1 OFS e;}} ($1 in A){print $2 OFS $1 OFS A[$1]}'

Hello kenshinhimura,

Hope following may help you.
Code:
awk 'NR==FNR{
if($0 ~ /Name/)                ######## Looking for String Name in lines #######
{$1="";e=$0;                ######## Making $1 as zero and saving line 0 this into a variable e ########
getline;A[$4]=$3 OFS $1 OFS e;}}    ######## going to next line then making an array with index $4 (IPAddress) and it's values is $3 $1 and variable e(which has rest of line except $1 of first line) 
($1 in A){print $2 OFS $1 OFS A[$1]}    ######## Fetching the contents of array A and printing the $2 and A[$1] (This condition will be executed when file2 will be read #####
' file1 file12

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 11  
Old 09-22-2014
i got you..thank you

---------- Post updated at 02:12 PM ---------- Previous update was at 01:59 PM ----------

Singh, i dont need this now.. im not sure if im gonna need this someday.

what if there is 3 files? im just confuse about the code below for FILE2... what if there is 3 files or 4 or 5? will the code will go through? jsut by adding? of another code below?
Code:
($1 in A){print $2 OFS $1 OFS A[$1]}    ######## Fetching the contents of array A and printing the $2 and A[$1] (This condition will be executed when file2 will be read #####

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge and Sort tabular data from different text files

I have 42 text files; each containing up to 34 lines with following structure; file1 H-01 23 H-03 5 H-05 9 H-02 14 . . file2 H-01 17 H-02 43 H-04 7 H-05 8 H-03 7 . . file3 (6 Replies)
Discussion started by: Syeda Sumayya
6 Replies

2. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

3. Shell Programming and Scripting

awk merge two files

file1 AAA3:WWW1:DDD1:XXX8:DDD2:XXX9 AAA6:WWW2:FFF1:XXX130:FFF1:XXX104:FFF1:XXX16 AAA7:WWW3:ZZZ1:XXX4:ZZZ2:XXX5:ZZZ3:XXX6:ZZZ4:XXX7file2 XXX8:EEE1:EEE2 XXX9:KKK1:KKK2 XXX130:OOO1:OOO2 XXX104:PPP1:PPP2 XXX16:RRR1:RRR1 XXX4:UUU1:UUU2 XXX5:III1:III2 XXX7:JJJ1:JJJ2Result... (2 Replies)
Discussion started by: vikus
2 Replies

4. Shell Programming and Scripting

AWK to match and merge data from 2 files into 1.

Hello, hopefully this is an easy on for the AWK guru's out there. I'm having some trouble figuring out how to match+merge data in 2 files into 1 single report. I've got my 2 files filtered and delimited, just need to MATCH $3 in file1 to $1 in file2, then put $0 from File1 and $2+$3 from File2... (6 Replies)
Discussion started by: right_coaster
6 Replies

5. Shell Programming and Scripting

Merge 2 CSV files using sed

Help in writing a script using sed which updates fileOne with the contents from fileTwo Example: Contents of fileOne 1,111111 2,897823 3,235473 4,222222 Contents of fileTwo 1,111111,A,1,2 4,222222,A,2,2 5,374632,A,3,2 6,374654,A,4,2 Final File should be: 1,111111,A,1,2... (9 Replies)
Discussion started by: NewToSed
9 Replies

6. Shell Programming and Scripting

please help to merge file with awk or sed

hi experts please help me,thanks in advance file1 arch : x86 install : pass make os : pass make build kernel : pass=100 failed=45 usb storage pass : The Linux Kernel Archives file2 arch : ppc install : failed make os : http://kernel.org (6 Replies)
Discussion started by: yanglei_fage
6 Replies

7. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

8. Shell Programming and Scripting

Merge 70 files into one data matrix

Hi, I have a list of 70 files in a directory and I need to merge the content of each file into one big matrix file (71 columns x 3060 rows). Each file has the following format only two columns per file: unique identifier1 randomtext1 randomtext1 a 5 b 3 c 6 d 3 e 2... (11 Replies)
Discussion started by: labrazil
11 Replies

9. Shell Programming and Scripting

merge two files using awk

Hi Guys, I wonder whether is possible to merge two files using awk. I have two files one with 7 columns and another one with 9 columns and the first column on both files is identical so will be my key to merge the files. Any ideas.Thanks in advance. Harby. (2 Replies)
Discussion started by: hariza
2 Replies

10. UNIX for Advanced & Expert Users

use of sed over cat to merge files

I have three files, basically: file 1 - one line header file 2 - big data (approx 80GB) file 3 - a one line trailer the existing process cats these together i.e cat file 1 file 2 file 3 however... I was thinking, surely it could be more efficient to insert the header (file 1) on the... (2 Replies)
Discussion started by: miwinter
2 Replies
Login or Register to Ask a Question