我有一個.txt包含三個欄位的表格檔案,第三個欄位通常有多個單詞:
GO:0000002 Akt3 mitochondrial genome maintenance
GO:0000002 Mef2a mitochondrial genome maintenance
GO:0000002 Mgme1 mitochondrial genome maintenance
GO:0000002 Mpv17 mitochondrial genome maintenance
GO:0000002 Mrpl15 mitochondrial genome maintenance
我想使用 交換欄位 1 和 2 awk,但是當我運行命令時:
awk 'BEGIN{OFS="\t"}{print $2,$1,$3;}' file.txt
我得到:
Akt3 GO:0000002 mitochondrial
Mef2a GO:0000002 mitochondrial
Mgme1 GO:0000002 mitochondrial
Mpv17 GO:0000002 mitochondrial
為什么我沒有得到第三個欄位中的所有單詞,以及如何解決這個問題?
提前致謝
uj5u.com熱心網友回復:
有多種方法可以解決這個問題,但主要問題是輸出欄位分隔符已設定,但默認輸入欄位分隔符是制表符和空格。將它們都設定為選項卡應該給出你想要的輸出(或者通過交換前兩個欄位并列印該行的其余部分來使其更通用)
awk 'BEGIN{OFS="\t"; FS="\t";} {print $2,$1,$3;}' file.txt
uj5u.com熱心網友回復:
您正在列印 3 個欄位,這就是為什么您在輸出中只得到 3 個欄位的原因。您正在設定OFS一個選項卡,但不是FS。
您可以做的是僅使用一個變數f1來翻轉欄位 1 和欄位 2,例如在切換值時保存欄位 1 的值。
然后列印在1右括號后添加的整行以列印整行。如果您有更多列,則不必在列印時手動指定它們。
awk '
BEGIN{FS=OFS="\t"}
{f1 = $1; $1 = $2; $2 = f1;}1
' file.txt
輸出
Akt3 GO:0000002 mitochondrial genome maintenance
Mef2a GO:0000002 mitochondrial genome maintenance
Mgme1 GO:0000002 mitochondrial genome maintenance
Mpv17 GO:0000002 mitochondrial genome maintenance
Mrpl15 GO:0000002 mitochondrial genome maintenance
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495658.html
上一篇:pandascsvUnicodeDecodeError:'utf-8'codeccan'tdecodebyte0x81inposition162:invalidstart
