簡體   English   中英

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

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

我有一個制表符分隔的文本文件,就像這個小例子:

小例子:

#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

並想制作一個像這樣預期輸出的新文件,其中刪除第一行並按以下順序重新組織列:

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.

這是預期的輸出:

預期輸出:

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

我正在嘗試使用以下命令在AWK執行此操作:

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

但我得到的結果不像預期的輸出。 你知道如何修復代碼嗎?

我不確定您為什么要重新分配字段? 當我們可以簡單地打印它們時:

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

或添加一個制表符分隔的輸出使用:

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

或者

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

或者

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


如果 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