文件中多个输出的更改字母的Awk命令

2024-09-30 00:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个如下所示的输入文件:

input.txt

THISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

我有另一个文件,其中有我要更改的字母位置和我要更改的字母位置,例如:

textpos.txt

Position    Text_Change
1           A
2           B
3           X

(实际上会有大约10000个字母的变化)

我希望每个文本更改都有一个单独的输出文件,如下所示:

output1.txt

AHISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

下一个:

output2.txt

TBISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

下一个:

output3.txt

THXSISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

我想学习如何在一个awk命令和Python的方式,以及如何做到这一点,并想知道什么是最好的,最快的方式做到这一点?你知道吗

提前谢谢。你知道吗


Tags: 文件text文本txtinput字母方式position
3条回答

请您尝试以下操作(考虑到您的实际输入文件中会包含相同类型的数据)。这个解决方案应该处理错误Too many open files error while running awk command,因为我正在关闭awk代码中的输出文件。你知道吗

awk '
FNR==NR{
   a[++count]=$0
   next
}
FNR>1{
   close(file)
   file="output"(FNR-1)".txt"
   for(i=1;i<=count;i++){
      if($1==1){
         print $2 substr(a[i],2) > file
      }
      else{
         print substr(a[i],1,$1-1) $2 substr(a[i],$1+1) > file
      }
   }
}'  input.txt  textpos.txt

3个名为output1.txtoutput2.txtoutput3.txt的输出文件及其内容如下。你知道吗

cat output1.txt
AHISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT
cat output2.txt
TBISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT
cat output3.txt
THXSISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

解释:在此处添加上述代码的解释。你知道吗

awk '
FNR==NR{                                                       ##Condition FNR==NR will be TRUE when first file named input.txt is being read.
   a[++count]=$0                                               ##Creating an array named a whose index is increasing value of count and value is current line.
   next                                                        ##next will skip all further statements from here.
}
FNR>1{                                                         ##This condition will be executed when 2nd Input_file textpos.txt is being read(excluding its header).
   close(file)                                                 ##Closing file named file whose value will be output file names, getting created further.
   file="output"(FNR-1)".txt"                                  ##Creating output file named output FNR-1(line number -1) and .txt in it.
   for(i=1;i<=count;i++){                                      ##Starting a for loop from 1 to till count value.
      if($1==1){                                               ##Checking condition if value of 1st field is 1 then do following.
         print $2 substr(a[i],2) > file                        ##Printing $2 substring of value of a[i] which starts from 2nd position till end of line to output file.
      }
      else{
         print substr(a[i],1,$1-1) $2 substr(a[i],$1+1) > file ##Printing substrings 1st 1 to till value of $1-1 $2 and then substring from $1+1 till end of line.
      }
   }
}'  input.txt  textpos.txt                                     ##Mentioning Input_file names here.

使用awk,对第二个文件滥用FS="",使每个字母成为自己的一列:

$ awk '
NR==FNR {
    a[$1]=$2; next }  # hash positions and letters to a
{
    for(i in a)       # for all positions
        $i=a[i]       # replace the letters in them
}1' textpos FS="" OFS="" file
ABXSISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

另一种方法是使用forsubstra[]$0逐字符构建变量:

$ awk '
NR==FNR {
    a[$1]=$2; next }                       # hash textpos to a
{
    for(i=1;i<=length($1);i++)             # for each position in $0
        b=b ((i in a)?a[i]:substr($0,i,1)) # get char from a[] or $0, in that order
    print b; b=""                          # output and reset b for next round
}' textpos file
ABXSISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

使用gawk:

$ awk 'NR > 1 && FNR == NR { r[$1] = $2; next } { 
      for (i in r) { 
          print substr($0, 1, i - 1) r[i] substr($0, i + 1) > "output" i ".txt"
      }
  }' textpos.txt input.txt

相关问题 更多 >

    热门问题