2011-08-18 183 views
79

bash history命令很酷。我明白爲什麼它顯示行號,但有沒有辦法可以調用歷史命令並抑制行號?沒有行號的Bash歷史記錄

的這裏點是使用history命令,所以請不要回復cat ~/.bash_history

電流輸出:

529 man history 
    530 ls 
    531 ll 
    532 clear 
    533 cd ~ 
    534 history

Historical graphic source.

所需的輸出:

man history 
ls 
ll 
clear 
cd ~ 
history

Historical graphic source.

感謝大家爲您的出色解決方案。保羅的是最簡單的,將爲我工作,因爲我的bash的歷史大小設置爲2000.

我也想分享一個很酷的文章,我今天早上發現。它有我現在使用的,喜歡養重複條目出bash的歷史,並確保多的bash會話不會覆蓋歷史文件一對夫婦不錯的選擇:http://blog.macromates.com/2008/working-with-history-in-bash/

回答

132

試試這個:

$ history | cut -c 8- 
+0

我們能否管從'history'命令的輸出,而不是讀的文件? – cwd

+0

似乎工作!你能解釋它在做什麼嗎?如果數字是1 - 10,000,它會起作用嗎? – cwd

+0

這非常粗糙 - 你可以用sed或awk做得更好。這裏的「剪切」只是刪除每行的前7個字符。 –

3

history命令沒有壓縮行號的選項。你必須爲每個人都在暗示多個命令組合:

例子:

history | cut -d' ' -f4- | sed 's/^ \(.*$\)/\1/g' 
+0

如果你要通過'sed'運行它,那麼最初的剪切是多餘的 - 只需將它添加到表達式中即可。 – moopet

11

awk可以幫助:如果你有很長hsitory

history|awk '{$1="";print substr($0,2)}' 

This answer可能會失敗。

HTH

+0

哈哈,謝謝 - substr'非常簡單,我一直在使用'history | awk'{for(i = 2; i <= NF; i ++)printf(「%s」,$ i); print(「\ r」)}'' – geedoubleya

5

或者,你可以使用SED:

history | sed 's/^[ ]*[0-9]\+[ ]*//' 

使用別名,你可以把這個作爲你的標準(把它貼在你的.bash_profile):

alias history="history | sed 's/^[ ]*[0-9]\+[ ]*//'" 
+1

基本正則表達式中的'\ +'不符合POSIX標準。如果您的'sed'不支持非標準的'\ +'擴展名,請使用'\ {1,\}'。 –

2

$ hh -n

你可能想嘗試https://github.com/dvorka/hstr其中allo ws針對Bash歷史的「提示框樣式」過濾,其中(可選的)基於度量的排序即它是在向前和向後兩個方向上更有效和更快:

enter image description here

它可以很容易地結合到CTRL-R和/或Ctrl-S鍵

0

雖然切斷與-c選項適用於大多數實際目的,我認爲將管道歷史記錄爲awk將是更好的解決方案。例如:

history | awk '{ $1=""; print }' 

OR

history | awk '{ $1=""; print $0 }' 

這兩種解決方案的做同樣的事情。歷史的輸出被輸入到awk中。 Awk然後空出第一列,這對應於歷史命令輸出中的數字。這裏awk更方便,因爲您不必關心輸出數字部分中的字符數。

print $0相當於print,因爲默認設置是打印出現在行上的所有內容。鍵入print $0更明確,但您選擇哪一個取決於您。如果您使用awk打印文件(cat將更快地輸入而不是awk,但這是爲了說明一個要點),那麼使用與0123k一樣的print $0和簡單的print的行爲更爲明顯。

[實施例]使用awk來顯示文件的與內容$ 0

$ awk '{print $0}' /tmp/hello-world.txt 
Hello World! 

[實施例]使用AWK而不顯示文件的內容明確的$ 0

$ awk '{print}' /tmp/hello-world.txt 
Hello World! 

[EX]當歷史記錄線跨越多行時使用awk

$ history 
    11 clear 
    12 echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style." 

$ history | awk ' $1=""; {print}' 
clear 
echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style." 
-1

我正在通過此線程並找到soluti ons是好的。在嘗試使用awk解決方案時,發現歷史上如果存在多行命令或空格等,他們可能會面臨打印OP實際要求的挑戰。讓我舉一個例子,我這​​樣做了一個命令。

awk ' 
match ($0, /<property class="java.lang.String" name="WorkJobNumber" value="[0-9]*"\/>/) {sub (substr ($0, RSTART+63, RLENGTH-66), _) 
                         } 
1123 
' Input_file 

所以在歷史上它將會在1序列號來,但如果這是在解決方案/ s的忽視可能是一個問題。所以下面的例子可以避免特別是多行命令被運行的情況。

history | awk '{Q=$1;sub(/^[[:space:]]+/,"",Q);if(LAST+1==Q && LAST && Q ~ /^[0-9]+/){;$1="";print;} else {print;};if(Q ~ /^[0-9]+/ && Q+0 == LAST+1 && $0 !~ /^$/){LAST=Q+0};next} (Q ~ /^[0-9]+/ && $0 !~ /^$/){LAST=Q+0} 1' 

的溶液的非一個襯裏形式太如下:

history | awk '{ 
     Q=$1; 
     sub(/^[[:space:]]+/,"",Q); 
     if(LAST+1==Q && LAST && Q ~ /^[0-9]+/){; 
          $1=""; 
          print; 
           } 
       else         { 
          print; 
            }; 
       if(Q ~ /^[0-9]+/ && Q+0 == LAST+1 && $0 !~ /^$/){ 
           LAST=Q+0 
             }; 
       next 
       } 
       (Q ~ /^[0-9]+/ && $0 !~ /^$/){ 
          LAST=Q+0 
              } 
       1 
       ' 

的上面的代碼說明作爲太如下(一個不應該運行以下,因爲它僅用於解釋的目的):

history |           #### Running history command here and using pipe to use this command's standard output to standard input for next awk command. 
awk '            #### Starting awk command from here. 
{Q=$1            #### Starting a variable named Q, whose value is $1 of current line. 
sub(/^[[:space:]]+/,"",Q)       #### subsitute initial space of variable Q(which has $1 of current line too) to NULL now. 
if(LAST+1==Q && LAST && Q ~ /^[0-9]+/)    #### mentioning here a if condition which is checking following conditions. 
               i- check if variable named LAST's value +1 is equal to variable Q's value(where LAST variable is the variable which has previous line's number. 
               ii- check LAST's value should NOT be NULL. 
               iii- check variable Q's value should be always starting from digits(As running multi-lie commands eg--> awk you could have commands there so making sure our LAST variable doesn't have any junk in it). 
{;$1="";print;}         #### making $1's value NULL so that history number will not print and then printing the current line. 
else            #### Mentioning else, in case above if condition is NOT TRUE then following statements should execute. 
{;print}           #### simply print the current line. 
;if(Q ~ /^[0-9]+/ && Q+0 == LAST+1 && $0 !~ /^$/) #### checking following conditions here. 
               i- check if variable Q's value always starts from digits to make sure no junk will come apart from history numbers in variable here. 
               ii- check Q+0 == LAST+1, here Q+0 I am mentioning because in my history it was showing 772*(in a row) so Q+0 will make sure only numbers are being captured here. then comparing it with LAST+1 value, if both are equal here. 
               iii- Making sure each line starts NOT from a space(in multi-line commands you may see spaces). 
{LAST=Q+0};next}         #### Assigning variable LAST's value to Q's value(by doing Q+0 making sure like only digits should be captured here). Mentioning next here which is awk's in-built keyword and will skip all next statements then. 
(Q ~ /^[0-9]+/ && $0 !~ /^$/)      #### This condition will be executed when previous are not true, checking Q's value should be starting from digits only to get the history's sequence number only and checking a line shouldn't start from space etc. 
{LAST=Q+0}           #### Assigning variable LAST's value to value of Q+0 which will make sure only digits should come. 
1'             #### So awk works on pattern{action} method, so by mentioning 1 I am making a pattern/condition true and then not mentioning any action here so by default print action will happen and it will print the current line, it will happen only in those cases when a history doesn't have any sequence number in it. 

雖然這個命令也可能有一些挑戰,盡我所能,保持完美無缺,反饋或建議。

5

我很清楚,這個問題是bash和許多人寧願不轉的zsh(線索downvotes ...)

不過,如果你願意切換到的zsh隨後的zsh支持這一本身(以及其他選項歷史格式化)

zsh> fc -ln 0 

(見https://serverfault.com/questions/114988/removing-history-or-line-numbers-from-zsh-history-file

+4

其實'fc'也是一個'bash'內建的。唯一的區別是第一行是'1',所以它會是'fc -ln 1' – wisbucky

+0

//有一個upvote的事實,我甚至不知道有什麼替代Bash的時間最長,直到有人在這種情況下提出了他們。 –

4

我來晚了就這一個,但更短的方法是添加以下在~/.bashrc~/.profile文件:

HISTTIMEFORMAT="$(echo -e '\r\e[K')"

從慶典manpage

  HISTTIMEFORMAT 
       If this variable is set and not null, its value is used as a 
       format string for strftime(3) to print the time stamp associated 
       with each history entry displayed by the history builtin. If 
       this variable is set, time stamps are written to the history 
       file so they may be preserved across shell sessions. This uses 
       the history comment character to distinguish timestamps from 
       other history lines. 

使用這個功能,智能黑客在於使變量 「打印」 回車(\r),並明確該行(ANSI代碼K)而不是實際的時間戳。

+0

稍微簡單一點,使用更加難懂的語法:'HISTTIMEFORMAT = $'\ r \ e [K'' – wjandrea

+0

和一行選項:'HISTTIMEFORMAT = $'\ r \ e [K'history' – wjandrea

1

可以使用命令cut來解決它:

剪下從標準輸入或文件的字段。

  • 剪出標準輸入的每一行的第一個十六個字符: cut -c 1-16

  • 切出所給文件的每一行的第一個十六個字符: cut -c 1-16 file

  • 切出一切從第3個字符到每行的結尾: cut -c3-

  • 切出每一行的第五字段,使用冒號作爲字段分隔符(缺省定界符是選項卡): cut -d':' -f5

  • 切出每一行的第二和第10字段,使用分號作爲分隔符: cut -d';' -f2,10

  • 通過每一行的7切出的字段3,使用空間作爲分隔符: cut -d' ' -f3-7

+0

//,你請舉個例子和輸出? –