1

I have a text files in ascii format and wanted to replace a specific value by value saved in another text file.

consider a file of name text_1. . . . 50.asc 5 columns, 4 rows

sample data in a file

 0.40007 0.544 0.6795 0.1545 -3.4028
 0.61488 0.8471 0.7444 0.3537 0.0709
 0.65128 0.6651 0.7948 0.9200 0.893
 0.70952 0.5990 0.5061 0.610 0.893

And I wanted to replace (5th column, 1st row) of each file by value same in Replacing_values.txt.

It have data

1
2
3
4
.
.
.
50

Expected result (continued to all files)

 0.40007 0.544 0.6795 0.1545  1
 0.61488 0.8471 0.7444 0.3537 0.0709
 0.65128 0.6651 0.7948 0.9200 0.893
 0.70952 0.5990 0.5061 0.610 0.893

I have tried this

for i in `seq 50`; do x=`awk 'FNR==(1) {print $5}' *.asc`; y=`cat Replacing_values.txt`; echo $x==$y ;done
2
  • 4th column, 1st row - you meant 5th column? Why comparing values echo $x==$y ? Commented Nov 20, 2019 at 12:25
  • Yes 5th column, sorry I didn't know how to do it, just randomly doing it. I am new in it. If you can help in this regard. I'll be thankful to you Commented Nov 20, 2019 at 12:34

1 Answer 1

1

With flexible GNU awk features:

gawk -i inplace -v repl="Replacing_values.txt" 'FNR==1{ getline $5 < repl }1' *.asc
  • -i inplace - allows to modify the input file(s) in-place
  • -v repl="Replacing_values.txt" - a variable keeping the filename with replace values
  • FNR==1 - consider only the 1st line of each input file
  • getline $5 < repl - read next record from repl file and assign it to the 5th column $5
4
  • It worked well, Thank you so much, Sir. Commented Nov 20, 2019 at 14:05
  • @MuhammadUmar, you're welcome! Commented Nov 20, 2019 at 14:06
  • ... note that the order in which the shell expands *.asc will not necessarily correspond to the order of lines in Replacing_values.txt - you might want to use brace expansion text_{1..50}.asc instead Commented Nov 20, 2019 at 14:07
  • @steeldriver, that's a good reminder Commented Nov 20, 2019 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .