Subsitute string in file 1 by line n of file 2


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Subsitute string in file 1 by line n of file 2
# 1  
Old 06-24-2018
Subsitute string in file 1 by line n of file 2

Hello,

My OS is Windows 10 and I am using Cygwin.

The content of file 1 is:
XXX string1

The content of file 2 is
4.156
2.312
3.527
1.687
etc....

I need to replace string 1 in file 1 by the content of line n in file 2.

So the expected output is e.g.:
XXX 3.527 (for n = 3)

I have the following script:
Code:
sed "s/string1/$(cat file2.txt)/" file1.txt > file3.txt

How can I adapt it to get the replacement of string 1 by the nth line of file 2?

Thanks in advance.
# 2  
Old 06-24-2018
Hello supernono06,

Could you please try following and let me know if this helps.

Code:
awk -v n=3 'FNR==NR{a[FNR]=$0;next} {sub("string1",a[n])} 1' file2  file1

OR in case you directly want to assign value from file2 to file1 on 2nd field of file1 then following may help you.

Code:
 awk -v n=3 'FNR==NR{a[FNR]=$0;next} {$2=a[n]} 1' file2  file1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-24-2018
Hello RavinderSingh13,

It works perfectly, many thanks.

If n is large, is there a way to use a loop to do the following:

Code:
 
awk -v n=1 'FNR==NR{a[FNR]=$0;next} {sub("string1",a[n])} 1' file2  file1
awk -v n=2 'FNR==NR{a[FNR]=$0;next} {sub("string2",a[n])} 1' file2  file1
awk -v n=3 'FNR==NR{a[FNR]=$0;next} {sub("string3",a[n])} 1' file2  file1
etc.…

Thanks.
# 4  
Old 06-24-2018
Quote:
Originally Posted by supernono06
Hello RavinderSingh13,
It works perfectly, many thanks.
If n is large, is there a way to use a loop to do the following:
Code:
 
awk -v n=1 'FNR==NR{a[FNR]=$0;next} {sub("string1",a[n])} 1' file2  file1
awk -v n=2 'FNR==NR{a[FNR]=$0;next} {sub("string2",a[n])} 1' file2  file1
awk -v n=3 'FNR==NR{a[FNR]=$0;next} {sub("string3",a[n])} 1' file2  file1
etc.…

Thanks.
Hello supernono06,

You could HIT THANKS button left most corner of any post if you feel it helped you Smilie. Now coming to your problem, no need to use a loop for this. There could be 2 situations for this.

1st: To substitute all the string keywords in file1 in that case following may help you.
Code:
awk  'FNR==NR{a[FNR]=$0;next} {sub("string1",a[FNR])} 1' file2  file1

Above will change them as per file2's line number so let's say string keywords comes on 1st, 3rd line of file1 so it will substitute string keyword with respective line's value from file2 in file1.

2nd: In case you want to give multiple values of line numbers which you want to change in file1, then following may help you.
Code:
awk -v n="1,4"  'BEGIN{num=split(n,array,",")} FNR==NR{a[FNR]=$0;next} /string/{count=count==num?1:++count;gsub("string",a[array[count++]])} 1' file2  file1

So in this case line number(s) 1st and 4th from file2's value will substituted on file1's keyword string. Also it will not bother line number on file1 side(asap a string (string) comes it will substitute it).

Thanks,
R. Singh
# 5  
Old 06-24-2018
Hello RavinderSingh13,

Thanks, but it is not doing what I need, I will explain it better.

The file 1 content is:
…..
XXX string1
….
YYY string2
...
ZZZ string3
...
AAA string4
….
The file 2 content is:
3.333
4.251
1.121
5.456

The expected output is:
...
XXX 3.333
...
YYY 4.251

ZZZ 1.121
...
AAA 5.456


Thanks in advance.

Supernono06

Last edited by supernono06; 06-24-2018 at 12:21 PM..
# 6  
Old 06-24-2018
Hello supernono06,

Could you please try following and let me know if this helps you.

Code:
 awk 'FNR==NR{a[FNR]=$0;next} {print $1,a[FNR]}'  Input_file2  Input_file1

Thanks,
R. Singh
# 7  
Old 06-24-2018
Hello RavinderSingh13,

Thanks but it is not working. With this script, the replacement takes place in the second field of the four 4th lines of the files, and all other 2nd fields of the remaining lines are removed.

I need to mention that there are lines above and below the strings I need to replace.

Supernono06
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If the 1th column of file f1 and file f2 is the same, then export those line with maximum string of

please help to write a awk command-line programs to achieve the following functions: Thank in advance. Requeset Description: compare two files f1 and f2, export to file f3: 1 Delete duplicate rows of in file f1 and file f2 2 If the 1th column of file f1 and file f2 is the same, then export... (1 Reply)
Discussion started by: weichanghe2000
1 Replies

2. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

3. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

4. Shell Programming and Scripting

Compare two string in two separate file and delete some line of file

Hi all i want to write program with shell script that able compare two file content and if one of lines of file have # at the first of string or nothing find same string in one of two file . remove the line in second file that have not the string in first file. for example: file... (2 Replies)
Discussion started by: saleh67
2 Replies

5. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

6. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

Subsitute from a position till end of line.

Hi, Having a following file's content, lets say: ABC|ANA|LDJ|||||DKD|||||| AJJ|KKDD||KKDK|||||||||||| KKD||KD|||LLLD||||LLD||||| Problem: Need to replace pipes from 8th occurrence of pipe till end. so the result should be: ABC|ANA|LDJ|||||DKD AJJ|KKDD||KKDK|||| ------- ------- ... (12 Replies)
Discussion started by: _Noprofi
12 Replies

9. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies
Login or Register to Ask a Question