Skip to main content

Replace a string in a column of a text file based on matching string listed in another file using linuxLinux

I have multiple sets of files, so I grepped the list from a column in a seperateseparate file such as file1.txtfile1.txt. I want to change all other files by using this file1.txtfile1.txt based on a matching string.

forFor example, I am pasting three files here

File 1:

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted the required information using grep; the command is given below:

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txtthese file1.txt and file2.txtfile2.txt to change another filesfile. Output will be like if list in file1.txtfile1.txt matches then all matched lists should be substituted as seq1 similarly using the second file2file2.txt.txt I want to substitute all matching patterns as seq2.

For example the file is given here to be changed by using information from the previous file:

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

And final output after the change should be as follows:

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command:

for i in f2.txt; do
    grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $$i |
    awk '{$1="seq1";print}'
    grep -wf other.txt $i |awk|
    awk '{$1="seq2";print}'; '
done

2nd attempt:

grep -wf f1.txt f2.txt|sedtxt |
sed -e 's/seq[0-9]*/seq1/g'

Is this correct? If it is correct is there any more efficient way to do it? alsoAlso it is only printing to the console. I want the output and also changes in the main file. Kindly improve my scripts.

I appreciate yout time and help.

Replace a string in a column of a text file based on matching string listed in another file using linux

I have multiple sets of files, so I grepped the list from a column in a seperate file such as file1.txt. I want to change all other files by using this file1.txt based on a matching string.

for example I am pasting three files here

File 1:

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted the required information using grep; the command is given below:

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change another files. Output will be like if list in file1.txt matches then all matched lists should be substituted as seq1 similarly using the second file2.txt I want to substitute all matching patterns as seq2.

For example the file is given here to be changed by using information from the previous file:

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

And final output after the change should be as follows:

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command:

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

2nd attempt:

grep -wf f1.txt f2.txt|sed -e 's/seq[0-9]*/seq1/g'

Is this correct? If it is correct is there any more efficient way to do it? also it is only printing to the console. I want the output and also changes in the main file. Kindly improve my scripts.

I appreciate yout time and help.

Replace a string in a column of a text file based on matching string listed in another file using Linux

I have multiple sets of files, so I grepped the list from a column in a separate file such as file1.txt. I want to change all other files by using this file1.txt based on a matching string.

For example, I am pasting three files here

File 1:

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted the required information using grep; the command is given below:

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use these file1.txt and file2.txt to change another file. Output will be like if list in file1.txt matches then all matched lists should be substituted as seq1 similarly using the second file2.txt. I want to substitute all matching patterns as seq2.

For example the file is given here to be changed by using information from the previous file:

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

And final output after the change should be as follows:

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command:

for i in f2.txt; do
    grep -wf f1.txt $i |
    awk '{$1="seq1";print}'
    grep -wf other.txt $i |
    awk '{$1="seq2";print}'
done

2nd attempt:

grep -wf f1.txt f2.txt |
sed -e 's/seq[0-9]*/seq1/g'

Is this correct? If it is correct is there any more efficient way to do it? Also it is only printing to the console. I want the output and also changes in the main file. Kindly improve my scripts.

I appreciate yout time and help.

added 80 characters in body; edited title
Source Link
gringer
  • 15.1k
  • 5
  • 24
  • 83

Replace a string in a column of a text file based on matching string listed in another file using lynuxlinux

I have multiple sets of files, so I grepedgrepped the list from a column in a seperate file such as file1.txt. I want to change all other filefiles by using this file1.txt based on a matching string.

for example I am pasting three filefiles here File 1

File 1:

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted the required information using grep,grep; the command is given below:

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change in another files. Output will be like if list in file1.txt matchmatches then all matched listlists should be substituedsubstituted as seq1 similarly using the second file2.txt I want to substituesubstitute all matching patternpatterns as seq2.

forFor example the file is given here to be changed by using information from the previous file:

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

andAnd final output after changedthe change should be as follows:

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command:

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

2nd attemp isattempt:

grep -wf f1.txt f2.txt|sed -e 's/seq[0-9]*/seq1/g'

Is this correct, if? If it is correct is there any more efficient way to do it.? also I am getting printit is only printing to the console. I want the output and also changes in the main file. Kindly improve my scripts appreciate.

I appreciate yout time and help.

Replace a string in a column of a text file based on matching string listed in another file using lynux

I have multiple sets of files, so I greped the list from column in seperate file such as file1.txt. I want to change all other file by using this file1.txt based on matching string.

for example I am pasting three file here File 1

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted required information using grep, command is given below

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change in another files. Output will be like if list in file1.txt match then all matched list should be substitued as seq1 similarly using second file2.txt I want to substitue all matching pattern as seq2.

for example file is given here to be changed by using information from previous file

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

and final output after changed should be as follows

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

2nd attemp is

grep -wf f1.txt f2.txt|sed -e 's/seq[0-9]*/seq1/g'

Is this correct, if correct is there any more efficient way to do it. also I am getting print. I want output and also changes in main file. Kindly improve my scripts appreciate yout time and help

Replace a string in a column of a text file based on matching string listed in another file using linux

I have multiple sets of files, so I grepped the list from a column in a seperate file such as file1.txt. I want to change all other files by using this file1.txt based on a matching string.

for example I am pasting three files here

File 1:

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted the required information using grep; the command is given below:

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change another files. Output will be like if list in file1.txt matches then all matched lists should be substituted as seq1 similarly using the second file2.txt I want to substitute all matching patterns as seq2.

For example the file is given here to be changed by using information from the previous file:

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

And final output after the change should be as follows:

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command:

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

2nd attempt:

grep -wf f1.txt f2.txt|sed -e 's/seq[0-9]*/seq1/g'

Is this correct? If it is correct is there any more efficient way to do it? also it is only printing to the console. I want the output and also changes in the main file. Kindly improve my scripts.

I appreciate yout time and help.

added 76 characters in body
Source Link
Umar
  • 157
  • 5

I have multiple sets of files, so I greped the list from column in seperate file such as file1.txt. I want to change all other file by using this file1.txt based on matching string.

for example I am pasting three file here File 1

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted required information using grep, command is given below

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change in another files. Output will be like if list in file1.txt match then all matched list should be substitued as seq1 similarly using second file2.txt I want to substitue all matching pattern as seq2.

for example file is given here to be changed by using information from previous file

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

and final output after changed should be as follows

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

2nd attemp is

grep -wf f1.txt f2.txt|sed -e 's/seq[0-9]*/seq1/g'

Is this correct, if correct is there any more efficient way to do it. also I am getting print. I want output and also changes in main file. Kindly improve my scripts appreciate yout time and help

I have multiple sets of files, so I greped the list from column in seperate file such as file1.txt. I want to change all other file by using this file1.txt based on matching string.

for example I am pasting three file here File 1

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted required information using grep, command is given below

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change in another files. Output will be like if list in file1.txt match then all matched list should be substitued as seq1 similarly using second file2.txt I want to substitue all matching pattern as seq2.

for example file is given here to be changed by using information from previous file

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

and final output after changed should be as follows

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

Is this correct, if correct is there any more efficient way to do it. also I am getting print. I want output and also changes in main file. Kindly improve my scripts appreciate yout time and help

I have multiple sets of files, so I greped the list from column in seperate file such as file1.txt. I want to change all other file by using this file1.txt based on matching string.

for example I am pasting three file here File 1

chr - seq1 NZ_JAHWGH010000010.1 0 60562 green_a4
chr - seq3 NZ_JAHWGH010000012.1 0 466573 green_a4
chr - seq5 NZ_JAHWGH010000013.1 0 125526 green_a4
chr - seq39 NZ_CAJGBF010000006.1 0 211154 green_a4
chr - seq40 NZ_CAJGBF010000007.1 0 199119 green_a4
chr - seq41 NZ_CAJGBF010000008.1 0 190934 green_a4
chr - seq42 NZ_CAJGBF010000009.1 0 178251 green_a4

I extracted required information using grep, command is given below

grep "NZ_JAHWGH" file.txt |awk '{print $3}' >file1.txt
grep "NZ_CAJGBF" file.txt |awk '{print $3}' >file2.txt

Now I wanted to use this file1.txt and file2.txt to change in another files. Output will be like if list in file1.txt match then all matched list should be substitued as seq1 similarly using second file2.txt I want to substitue all matching pattern as seq2.

for example file is given here to be changed by using information from previous file

seq3 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq5 167934 169650 fill_color=green_a0
seq42 200621 202367 fill_color=red_a0
seq41 160164 161903 fill_color=green_a0
seq40 31356 33104 fill_color=green_a0

and final output after changed should be as follows

seq1 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0

I tried using this command

for i in f2.txt; do grep -wf f1.txt $i|awk '{$1="seq1";print}'; grep -wf other.txt $i |awk '{$1="seq2";print}'; done

2nd attemp is

grep -wf f1.txt f2.txt|sed -e 's/seq[0-9]*/seq1/g'

Is this correct, if correct is there any more efficient way to do it. also I am getting print. I want output and also changes in main file. Kindly improve my scripts appreciate yout time and help

added 1 character in body
Source Link
Umar
  • 157
  • 5
Loading
added 97 characters in body
Source Link
Umar
  • 157
  • 5
Loading
added 227 characters in body
Source Link
Umar
  • 157
  • 5
Loading
edited body
Source Link
Umar
  • 157
  • 5
Loading
Source Link
Umar
  • 157
  • 5
Loading