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
# 1  
Old 09-22-2014
Merge files and copy some data using sed or awk

Copy data from other file to paste

Code:
cat file1:

Name: server1.data.com
data1 server1 running

Name: server3.data.com
data3 server3 running


cat file2:
server1  good
server2  bad
network not ok
server3 good


Output:
good running data1 server1 server1.data.com
good running data3 server3  server3.data.com

# 2  
Old 09-22-2014
Hello kenshinhimura,

Following may help you.

Code:
awk 'NR==FNR{if($1 ~ /Name/){d=$2;gsub(/\..*/,X,$2);c=$2;getline;A[c]=$NF  OFS $(NF-1) OFS $(NF-2) OFS d}} ($1 in A){print $2 OFS A[$1]}' file1 file2
OR
awk 'NR==FNR{if($1 ~ /Name/){d=$2;gsub(/\..*/,X,$2);c=$2;getline;A[c]=$NF  OFS $1 OFS $2 OFS d}} ($1 in A){print $2 OFS A[$1]}' file1 file2

Output will be as follows.

Code:
good running data1 server1 server1.data.com
good running data3 server3 server3.data.com

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-22-2014
Singh, what if
Code:
Name: server1.data.com OTHER DATA IN IT(I WANT TO CAPTURE)
data1 OTHERs OTHERS  server1 running

# 4  
Old 09-22-2014
Hello kenshinhimura,

Not sure if following helps, but here is a approach.

Code:
cat file1
Name: server1.data.com Singh is King
data1 server1 running
Name: server3.data.com ravinder singh
data3 server3 running
 
cat file2
server1  good
server2  bad
network not ok
server3 good

Code is as follows.

Code:
awk 'NR==FNR{if($1 ~ /Name/){d=$2;gsub(/\..*/,X,$2);c=$2;$2=$1="";h=$0;getline;A[c]=$NF  OFS $(NF-1) OFS $(NF-2) OFS d OFS h}} ($1 in A){print $2 OFS A[$1]}' file1 file2

Output will be as follows.

Code:
good running server1 data1 server1.data.com   Singh is King
good running server3 data3 server3.data.com   ravinder singh

Thanks,
R. Singh
# 5  
Old 09-22-2014
Singh, do you mind explain them for me? and its relationship to file1 and file2. i wanna learn this command more. i use awk. but not as extensive as this.
# 6  
Old 09-22-2014
Hello,

Following may explain, I have just tried to explained the steps they are only explanation not exact code.

Code:
NR==FNR                                                                       ##### This condition will be true when first file will be read #####
if($1 ~ /Name/                                                                 ##### Looking for line's whose first field has word Name #######
d=$2;gsub(/\..*/,X,$2)                                                     ##### Saving $2 in a variable named d, then removing everything from .(dot) to till end of field 2 #######
c=$2                                                                                 ###### Saving this new value of $2 in a variable named c #######
$2=$1="";h=$0                                                                ##### Making $2 and $1 to NULL and saving rest f line to a variable named h ########
getline                                                                              ##### Moving onto next line now ########
A[c]=$NF  OFS $(NF-1) OFS $(NF-2) OFS d OFS h   ##### Now creating a array named A whose index is c(which has data manipulated $2 in step-4) 
                                                                                        ##### OFS is Output Field separator and NF is number of fields and these values are taken as per request #####
($1 in A)                                                                         ##### Now this statement will be executed when File2 is read as we have ended condition of NR==FNR before it ######
{print $2 OFS A[$1]}                                                    ##### printing $2 of file2 and array A's value#######


Last edited by RavinderSingh13; 09-22-2014 at 03:03 PM.. Reason: Tried to fix alignment good + Admin team could you please fix alignment of comments not able to do so
# 7  
Old 09-22-2014
Singh, i tried it in other data, i did not work.
can you check data below pleasE?
thanks

Code:
Data1:
Name  data W3
us-west-2b running i-21690a303 192.216.3.20   None
Name  outlook  M7
us-west-2a stopped i-2103a9ee8 192.216.1.47   None
Name  wow Master Device
us-west-2c running i-21c95a6e9 192.216.7.80   None
Name  MySQL S2
us-west-2b running i-21295a637 192.216.3.41   None

Data2:
192.216.7.80 server1.com
192.216.3.20  server2.com

Output:
server1.com  192.216.7.80   i-21c95a6e9  us-west-2b   wow Master Device

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