2013-12-09 52 views
0

我需要用連續數字列表替換第一列數字。該文件看起來像:Bash:用連續列表替換列中的數字列表

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的任何解決方案,將不勝感激。

+0

Sed不是最好的工具,因爲內部處理的湖泊除了用其他方式取代(不計數,...)。與簡單的命令相比awk可能但非常重要 – NeronLeVelu

回答

4

使用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這將永遠是真實和打印大的數字。
您可以使用'{$1=NR}1{$1=NR;print $0}使其更具可讀性。

+0

哇,你是更好的人..我不知道我們可以簡單地寫這個。 \米/ – Anubhab

0

嘗試使用awk變量NR,它給你正在處理的記錄號。
如果你想A樣品代碼見下文:
假設文件中有這樣的

1 ask 
5 rup 
6 dd 


記錄現在下面的命令運行這個命令:

awk '{ print NR FS $2 }' filename 


輸出:

1 ask 
2 rup 
3 dd 


其餘的我離開你找出