简体   繁体   English

使用 AWK 在制表符分隔的文本文件中应用一些更改

[英]Applying some changes in a tab separated text file using AWK

I have a tab separated text file like this small example:我有一个制表符分隔的文本文件,就像这个小例子:

small example:小例子:

#type   cNA NA  me  ion Dir Mism    Bulge
X   GAAGC   GAAGa   chr8    3997355     -   5   0
X   GAAGC   GAAGC   chr8    11720692    +   5   0
X   GAAGC   GAAGC   chr8    23414961    -   5   0

and would like to make a new file like this expected output in which the 1st line is removed and columns are re-organised in this order:并想制作一个像这样预期输出的新文件,其中删除第一行并按以下顺序重新组织列:

1) columns 1 and 8 are removed.
2) 2nd column (small example file) moved to 1st column (expected output).
3) 4th column moved to the 2nd column.
4) 5th column moved to 3rd column.
5) 3rd column moved to 4th column.
6) 6th column moved to 5th column.
7) 7th column moved to 6th column.

here is the expected output:这是预期的输出:

expected output:预期输出:

GAAGC   chr8    3997355     GAAGa   -   5
GAAGC   chr8    11720692    GAAGC   +   5
GAAGC   chr8    23414961    GAAGC   -   5

I am trying to do that in AWK using the following command:我正在尝试使用以下命令在AWK执行此操作:

awk -F '\t' '{ print $2 $4 $5 $3 $6 $7}' infile.txt > output.txt

but the results I am getting is not like expected output.但我得到的结果不像预期的输出。 do you know how to fix the code?你知道如何修复代码吗?

I am not sure why you are re-assigning the fields?我不确定您为什么要重新分配字段? When we can simply print them like:当我们可以简单地打印它们时:

awk 'FNR==1{next} {print $2,$4,$5,$3,$6,$7}' Input_file

OR to add a tab separated output use:或添加一个制表符分隔的输出使用:

awk 'BEGIN{OFS="\t"} FNR==1{next} {print $2,$4,$5,$3,$6,$7}' Input_file

OR或者

awk 'FNR>1{print $2,$4,$5,$3,$6,$7}' Input_file

or或者

awk 'BEGIN{OFS="\t"} FNR>1{print $2,$4,$5,$3,$6,$7}' Input_file


In case OP willing to use field re-assignment approach only then one could try following.如果 OP 愿意使用字段重新分配方法,则只能尝试以下方法。

awk '
BEGIN{
  OFS="\t"
}
FNR==1{
  next
}
{
  $1=$2
  $2=$4
  $4=$3
  $3=$5
  $5=$6
  $6=$7
  $7=$8=""
  sub(/ +$/,"")
}
1
'  Input_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM