0

I have a big problem. I have a file like:

   18   13    8    5        3       1.715   2.845   1.046  -5.607  -0.000   0.000
   38   34   29   24        3       1.715   2.845   1.046  -5.607  -0.000   0.000
   12   14    9    5        3       1.715   2.845   1.046  -5.607  -0.000   0.000
   34   29   24   30        3       1.715   2.845   1.046  -5.607  -0.000   0.000
   14    9    5    3        3       1.715   2.845   1.046  -5.607  -0.000   0.000
   13    8    5    3        3       1.715   2.845   1.046  -5.607  -0.000   0.000
   34   29   24   28        3       1.590   4.770   0.000  -6.360  -0.000   0.000
   13    8    5    7        3       1.590   4.770   0.000  -6.360  -0.000   0.000
   14    9    5    7        3       1.590   4.770   0.000  -6.360  -0.000   
    7    5    3    6        3       0.628   1.883   0.000  -2.510  -0.000   0.000
    7    5    3    4        3       0.628   1.883   0.000  -2.510  -0.000   0.000
    7    5    3    2        3       0.979   2.937   0.000  -3.916  -0.000   0.0000.000
   14    9    5    8        3      -6.458   0.810   5.682  -0.033  -0.000   0.000
   13    8    5    9        3      -6.458   0.810   5.682  -0.033  -0.000   0.000
   34   29   24   22        3      -6.458   0.810   5.682  -0.033  -0.000   0.000
etc

And I use gsub to replace specific number to specific number like:

awk '{ gsub(/1/, "9"); gsub(/2/, "8"); gsub(/3/, "6"); gsub(/4/, "7"); gsub(/5/, "4"); gsub(/6/, "10"); gsub(/7/, "5"); gsub(/82/, "92"); gsub(/83/, "91"); gsub(/84/, "94"); gsub(/85/, "93"); gsub(/86/, "97"); gsub(/87/, "95"); gsub(/88/, "96"); etc.....
print $1, $2, $3, $4 }' proba | tee proba2

But I have a problem. I want to replace for example 111 to 45, so I use gsub and have 45 in my document, then in my script I use again gsub and replace 4 to 76, and this replace 4 to 76, but also 45 to 765!! - I dont want that.

I try this code, but it doesnt work

awk '{ gsub(/ 1 /, " 9 "); gsub(/ 2 /, " 8 "); gsub(/ 3 /, " 6 "); gsub(/ 4 /, " 7 "); gsub(/ 5 /, " 4 "); gsub(/ 6 /, " 10 "); gsub(/ 7 /, " 5 "); gsub(/ 8 /, " 23 "); gsub(/ 9 /, " 3 "); gsub(/ 10 /, " 11 "); gsub(/ 11 /, " 12 ");  print $1, $2, $3, $4 }' proba | tee proba2

I want to replace whole numbers. I don't want 12 become 98. I want to replace whole number in four columns (print $1, $2, $3, $4). Input format for example: 22 56 3 75 Output format for example 35 82 91 5 It's good to have the same format as the input, but this is not so much important. (I can edit format a little after that by myself)

  • I want to replace whole numbers. I dont want 12 become 98. I want to replace whole number in four columns (prinst $1, $2, $3, $4). Input format for example: 22 56 3 75 Output format for example 35 82 91 5 Its good to have the same format as the input, but this is not so much important(I can edit format a little after that by myself) – Jakub Jun 17 at 17:04
2

I suggest to create a file, e.g. replace.txt that contains the replacements, old value in column 1, new value in column 2, like this

1 9
2 8
3 6
4 7
5 4
6 10
7 5
82 92
83 91
84 94
85 93
86 97
87 95
88 96

Assuming your data file is data.txt, you can use this AWK script:

awk 'NR==FNR { t[$1]=$2; next;} # fill translation table from first file
{ for(i=1;i<5;i++) { # process columns 1..4
        if ($i in t) { # replacement available
                $i=t[$i]; # replace field only once
        }
  }
  print $1, $2, $3, $4;
  # or if you want all columns:
  # $1=$1 # this makes sure unmodified lines get the new format
  # print # print all fields after modification
}' replace.txt data.txt

With your input this generates

18 13 8 4
38 34 29 24
12 14 9 4
34 29 24 30
14 9 4 6
13 8 4 6
34 29 24 28
13 8 4 5
14 9 4 5
5 4 6 10
5 4 6 7
5 4 6 8
14 9 4 8
13 8 4 9
34 29 24 22

or with the alternative output

18 13 8 4 3 1.715 2.845 1.046 -5.607 -0.000 0.000
38 34 29 24 3 1.715 2.845 1.046 -5.607 -0.000 0.000
12 14 9 4 3 1.715 2.845 1.046 -5.607 -0.000 0.000
34 29 24 30 3 1.715 2.845 1.046 -5.607 -0.000 0.000
14 9 4 6 3 1.715 2.845 1.046 -5.607 -0.000 0.000
13 8 4 6 3 1.715 2.845 1.046 -5.607 -0.000 0.000
34 29 24 28 3 1.590 4.770 0.000 -6.360 -0.000 0.000
13 8 4 5 3 1.590 4.770 0.000 -6.360 -0.000 0.000
14 9 4 5 3 1.590 4.770 0.000 -6.360 -0.000
5 4 6 10 3 0.628 1.883 0.000 -2.510 -0.000 0.000
5 4 6 7 3 0.628 1.883 0.000 -2.510 -0.000 0.000
5 4 6 8 3 0.979 2.937 0.000 -3.916 -0.000 0.0000.000
14 9 4 8 3 -6.458 0.810 5.682 -0.033 -0.000 0.000
13 8 4 9 3 -6.458 0.810 5.682 -0.033 -0.000 0.000
34 29 24 22 3 -6.458 0.810 5.682 -0.033 -0.000 0.000
  • (opinion based spoiler) : I would have use $i in t instead of t[$i] != "" – Archemar Jun 18 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.