0

I have a awk code for combining 2 files and add the result to the end of file.txt using ">>"

my code

NR==FNR && $2!=0 {two[$0]++;j=1; next }{for(i in two) {split(i,one,FS); if(one[3] == $NF){x=$4;sub( /[[:digit:]]/, "A", $4); print j++,$1,$2,$3,x,$4 | "column -t" ">>" "./Desktop/file.txt"}}}

i want put my awk to bash script and finaly sort my file.txt and save sorted result to file.txt again using >

i tried this

#!/bin/bash
command=$(awk '{NR==FNR && $2!=0 {two[$0]++;j=1; next }{for(i in two) {split(i,one,FS); if(one[3] == $NF){x=$4;sub( /[[:digit:]]/, "A", $4); print $1,$2,$3,$4 | "column -t" ">>" "./Desktop/file.txt"}}}}')
echo -e "$command" | column -t | sort -s -n -k4 > ./Desktop/file.txt 

but it gives me error "for reading (no such a file or directory)"

where is my mistake?

Thanks in advance

4

1 回答 1

1

1)您没有为 awk 脚本指定输入文件。这个:

command=$(awk '{...stuff...}')

需要是:

command=$(awk '{...stuff...}' file1 file2)

2)您将您的 awk 条件“NR == ...”移动到操作部分内,这样它就不再表现为条件。

3)您的 awk 脚本输出将进入“file.txt”,因此当您在下一行回显时,“command”为空。

4) 你有未使用的变量 x 和 j

5) 您将 arg FS 不必要地传递给 split()。

ETC...

我想你想要的是:

command=$( awk '
   NR==FNR && $2!=0 { two[$0]++; next }
   {
      for(i in two) {
          split(i,one)
          if(one[3] == $NF) {
             sub(/[[:digit:]]/, "A", $4)
             print $1,$2,$3,$4 
          }
      }
    }
' file1 file2 )
echo -e "$command" | column -t >> "./Desktop/file.txt"
echo -e "$command" | column -t | sort -s -n -k4 >> ./Desktop/file.txt

但很难说。

于 2012-10-22T18:28:21.077 回答