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
# 8  
Old 06-24-2018
Quote:
Originally Posted by supernono06
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
Hello supernono06,

So I am assuming 2 things here.

1st: You want to do replacement wherever string keyword is found on lines of Input_file1.

2nd: I am assuming that in case number of lines are lesser in Input_file2 than Input_file1(means keywords strings are coming more than the numbers in Input_file2) then once it reaches it last value(from Input_file2) then again it should start from beginning in Input_file2 values.

Code:
awk 'FNR==NR{a[FNR]=$0;val++;next} {count==val?"":count} /string/{print $1,a[++count];next} 1'  Input_file2  Input_file1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 06-24-2018
Thanks a lot RavinderSingh13.

This works. I have another file 1 with content:
...
set title "XXX YYY I = 3 mol L^{-1} pH = string1"
...
set title "XXX YYY I = 2 mol L^{-1} pH = string2"
...
set title "XXX YYY I = 1.5 mol L^{-1} pH = string3"
...
set title "XXX YYY I = 1 mol L^{-1} pH = string4"
...
With your actual script, the output is:
...
set 3.333
...
set 4.251
...
set 1.121
...
set 5.456
...
I would like to obtain the following output please:
...
set title "XXX YYY I = 3 mol L^{-1} pH = 3.333"
...
set title "XXX YYY I = 2 mol L^{-1} pH = 4.251"
...
set title "XXX YYY I = 1.5 mol L^{-1} pH = 1.121"
...
set title "XXX YYY I = 1 mol L^{-1} pH = 5.456"
...
Thanks in advance.

Supernono06
# 10  
Old 06-24-2018
Hello supernono06,

For this shown Input_file1, could you please try following and let me know if this helps you.
Code:
awk 'FNR==NR{a[FNR]=$0;val++;next} {count==val?"":count} /string/{sub(/string[0-9]+/,a[++count])} 1'  Input_file2  Input_file1

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

Thanks a lot. It works very well.

Here is my last question.

I have another file 2 with content:
3.333 #3M
4.251 #2M
1.121 #1.5M
5.456 #1M

With the actual script, the output is:
...
set title "XXX YYY I = 3 mol L^{-1} pH = 3.333 #3M"
...
set title "XXX YYY I = 2 mol L^{-1} pH = 4.251" #2M"
...
set title "XXX YYY I = 1.5 mol L^{-1} pH = 1.121 #1.5M"
...
set title "XXX YYY I = 1 mol L^{-1} pH = 5.456 #1M"


Please, is it possible to adapt the script to get:
...
set title "XXX YYY I = 3 mol L^{-1} pH = 3.333"
...
set title "XXX YYY I = 2 mol L^{-1} pH = 4.251"
...
set title "XXX YYY I = 1.5 mol L^{-1} pH = 1.121"
...
set title "XXX YYY I = 1 mol L^{-1} pH = 5.456"


Thanks a lot.

Supernono06
# 12  
Old 06-24-2018
Hello supernono06,

Please do wrap your samples in CODE TAGS(see how my command looks similarly see co de button while editing your post and wrap your examples in it). Could you please try following then and let me know if this helps you.

Code:
awk 'FNR==NR{a[FNR]=$1;val++;next} {count==val?"":count} /string/{sub(/string[0-9]+/,a[++count])} 1'  Input_file2  Input_file1

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

Thanks a lot. Just a last thing please.

I have another file 1 as follows:

Code:
...
XXX; YYY; ZZZ; AAA string1   BBB; END
XXX; YYY; ZZZ; AAA string1   BBB; END
...
XXX; YYY; ZZZ; AAA string2   BBB; END
XXX; YYY; ZZZ; AAA string2   BBB; END
...
XXX; YYY; ZZZ; AAA string3   BBB; END
XXX; YYY; ZZZ; AAA string3   BBB; END
...
XXX; YYY; ZZZ; AAA string4   BBB; END
XXX; YYY; ZZZ; AAA string4   BBB; END
...

With the actual script, I get:

Code:
 
XXX; YYY; ZZZ; AAA 3.333   BBB; END
XXX; YYY; ZZZ; AAA 4.251   BBB; END
XXX; YYY; ZZZ; AAA 1.121   BBB; END
XXX; YYY; ZZZ; AAA 5.456   BBB; END
XXX; YYY; ZZZ; AAA         BBB; END
XXX; YYY; ZZZ; AAA         BBB; END
....

The strings are replaced in the four first lines and become empty for all other lines.

I would need the following output please:
Code:
...
XXX; YYY; ZZZ; AAA 3.333   BBB; END
XXX; YYY; ZZZ; AAA 3.333   BBB; END
...
XXX; YYY; ZZZ; AAA 4.251   BBB; END
XXX; YYY; ZZZ; AAA 4.251   BBB; END
...
XXX; YYY; ZZZ; AAA 1.121   BBB; END
XXX; YYY; ZZZ; AAA 1.121   BBB; END
...
XXX; YYY; ZZZ; AAA 5.456   BBB; END
XXX; YYY; ZZZ; AAA 5.456   BBB; END
...

Thanks again.

Supernono06
# 14  
Old 06-24-2018
Hello supernono06,

Again assuming that your Input_file(s) are same as shown ones, if yes then following may help you here.

Code:
awk 'FNR==NR{a[FNR]=$1;val++;next} {count=count==val?0:count} match($0,/string[0-9]+/){prev=val=substr($0,RSTART,RLENGTH);if(!array[prev]++){count++};sub(val,a[count])} 1'  Input_file2   Input_file1

Adding a non-one liner form of solution too here.

Code:
awk '
FNR==NR{
  a[FNR]=$1;
  val++;
  next
}
{
  count=count==val?0:count
}
match($0,/string[0-9]+/){
  prev=val=substr($0,RSTART,RLENGTH);
  if(!array[prev]++){ count++ };
  sub(val,a[count])
}
1
'  Input_file2   Input_file1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
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