Replace file contents from another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace file contents from another
# 1  
Old 03-07-2013
Replace file contents from another

Hi Friends,

I have a file1 with 5 columns

Code:
a b c d e
f g h i j

I have file2 with 3 columns

Code:
1 2 3
4 5 6

I want to replace 3rd 4th and 5th columns in file1 with file2 contents, so the output would be

Code:
a b 1 2 3
f g 4 5 6

Thanks
# 2  
Old 03-07-2013
Code:
awk 'FNR == NR { a[NR] = $1 FS $2; next} { print a[FNR], $0 } ' file1 file2

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-07-2013
Quote:
Originally Posted by bipinajith
Code:
awk 'FNR == NR { a[NR] = $1 FS $2; next} { print a[FNR], $0 } ' file1 file2

Thanks Bipin.

But, what if I have to only copy 2nd and 3rd column from file2 to 1st and second columns in file1.

How do I change the code? Is there a way to see all the column numbers in the code, so that when I work on my original data with thousands of columns, I could easily go back and forth?

Thanks for your time.
# 4  
Old 03-07-2013
Easiest solution is to just use paste followed by either cut or awk, depending on the complexity of the selection process.

Your original problem doesn't require anything as clever as awk(assuming that the fields are delimited by a single space):
Code:
paste -d ' ' file1 file2 | cut -d ' ' -f 1,2,6-8

If you need to get fancier (cutcan't reorder), then paste before piping into awk(in the following code, replace the awk field numbers with the ones that you actually need):
Code:
paste file1 file2 | awk '{print $5, $6, $1, $3}'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 03-07-2013
Quote:
Originally Posted by alister
Easiest solution is to just use paste followed by either cut or awk, depending on the complexity of the selection process.

Your original problem doesn't require anything as clever as awk(assuming that the fields are delimited by a single space):
Code:
paste -d ' ' file1 file2 | cut -d ' ' -f 1,2,6-8

If you need to get fancier (cutcan't reorder), then paste before piping into awk(in the following code, replace the awk field numbers with the ones that you actually need):
Code:
paste file1 file2 | awk '{print $5, $6, $1, $3}'

Regards,
Alister
Thanks Allister.

But, my original files has more than thousands of columns.

So, I would like to specifically choose columns from my file2 and replace them at specific columns in file1.

That was the reason why I would like to see the columns in the code, so that I could always go back and forth changing them.
# 6  
Old 03-07-2013
Quote:
Originally Posted by jacobs.smith
Is there a way to see all the column numbers in the code, so that when I work on my original data with thousands of columns, I could easily go back and forth?
You should provide a sample of the output format which you seek.

Regards,
Alister

---------- Post updated at 09:44 AM ---------- Previous update was at 09:40 AM ----------

This may be of use to you. It prints each line and column number followed by the value at that position:
Code:
awk '{for (i=1; i<=NF; i++) print NR "," i ": " $i}'

Regards,
Alister
# 7  
Old 03-07-2013
Quote:
Originally Posted by jacobs.smith
But, what if I have to only copy 2nd and 3rd column from file2 to 1st and second columns in file1.

How do I change the code? Is there a way to see all the column numbers in the code, so that when I work on my original data with thousands of columns, I could easily go back and forth?
In that case use a for loop in your awk program:
Code:
awk ' FNR == NR {
        for ( i = 3; i <= NF; i++ )
                a[NR] = a[NR] FS $i
        next
}
{
        for ( i = 2; i <= 3; i++ )
                printf "%d ", $i
        printf "%s ", a[FNR]
        for ( i = 4; i <= NF; i++ )
                printf "%d ", $i
        printf "\n"
} ' file1 file2

Modify the for loop indices as per your requirement.
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

2. Shell Programming and Scripting

Replace contents of a file

Hello, I need help to replace a value by a new one. I've got a script, that will get directory size in ko, then write the size value return in a log file : The patch is given in crontab, and use with $1 in the script. I am looking for help to replace, after: the old value without to... (6 Replies)
Discussion started by: Aswex
6 Replies

3. UNIX for Dummies Questions & Answers

How To Replace Contents in a File?

How can i replace the contents in a particular line of a file. <FOLDERMAP SOURCEFOLDERNAME="FFCB-2012" SOURCEREPOSITORYNAME="Repo_DEV" TARGETFOLDERNAME="TEST" TARGETREPOSITORYNAME="Dev_Repo"/> For Example I want to replace the SOURCEREPOSITORYNAME="Repo_DEV" to... (3 Replies)
Discussion started by: Ariean
3 Replies

4. Shell Programming and Scripting

Replace Contents from One file into another

Hi Friends, I have two input files cat input1 chr1 100 200 chr1 200 300 chr2 300 400 cat input2 chr1 hello monday 10 20 . - . sometext chr1 hello monday 20 30 . - . sometext chr2 hello monday 30 40 . - . sometext Now, I want to replace $1, $4 and $5 of input2 with $1, $2 and... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

6. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

7. Shell Programming and Scripting

How to replace specific contents in a file?

From the existing file, I need to replace specific contents possibly with var every time when the user changes the var. e.g the contents in the file file.txt is 'My name is $n and I am $y years old' and every time user changed the var outside the file, the contents of the file should be created... (4 Replies)
Discussion started by: Emilywu
4 Replies

8. Shell Programming and Scripting

Script to check a file and replace some of the contents

Hi I have a file that looks like this: Line 0 animal elephant Line 1 animal elephant Line 2 animal elephant Line 3 animal elephant What i am aiming to do is with a script and an input value of... (6 Replies)
Discussion started by: tara
6 Replies

9. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies

10. Shell Programming and Scripting

Replace contents of a file

Hi, I want to replace the contents of a file.I tried using : sed 's/01514581/01514582/' $p where 01514581 is the original value 01514582 is the replaced value $p is the file name (captured in a variable).. The output does not recognise $p If you give : sed... (2 Replies)
Discussion started by: shiroh_1982
2 Replies
Login or Register to Ask a Question