![](/img/trans.png)
[英]How to replace list of numbers in column for random numbers in other column in BASH environment
[英]Bash: replace list of numbers in column with sequential list
我需要用序列號列表替換數字的第一列。 該文件如下所示:
1 the quick brown fox jumps over
7 the lazy dog the quick brown
13 fox jumps over the lazy dog
26 the quick brown fox jumps over
31 the lazy dog the quick brown
我需要:
1 the quick brown fox jumps over
2 the lazy dog the quick brown
3 fox jumps over the lazy dog
4 the quick brown fox jumps over
5 the lazy dog the quick brown
任何使用bash / sed / awk的解決方案將不勝感激。
使用awk
awk '$1=NR' OFS="\t" file
1 the quick brown fox jumps over
2 the lazy dog the quick brown
3 fox jumps over the lazy dog
4 the quick brown fox jumps over
5 the lazy dog the quick brown
由於NR
總是大於0
的數字,因此始終為true並打印出來。
您可以使用'{$1=NR}1
或{$1=NR;print $0}
使其更具可讀性。
嘗試使用awk變量NR
,該變量為您提供正在處理的記錄號。
如果您需要示例代碼,請參見以下內容:
假設文件具有這樣的記錄
1 ask
5 rup
6 dd
現在運行以下命令:
awk '{ print NR FS $2 }' filename
輸出:
1 ask
2 rup
3 dd
休息,我留給你弄清楚
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.