IT story

줄 번호가없는 배쉬 히스토리

hot-time 2020. 7. 27. 07:28
반응형

줄 번호가없는 배쉬 히스토리


bash history명령은 매우 멋집니다. 줄 번호가 표시되는 이유를 이해하지만 history 명령을 호출하고 줄 번호를 표시하지 않는 방법이 있습니까?

요점은 history 명령을 사용하는 것이므로 답장하지 마십시오. cat ~/.bash_history

전류 출력 :

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

역사적 그래픽 소스.

원하는 출력 :

man history
ls
ll
clear
cd ~
history

역사적 그래픽 소스.

훌륭한 솔루션을 제공해 주신 모든 분들께 감사드립니다. 내 배쉬 히스토리 크기가 2000으로 설정되어 있기 때문에 Paul이 가장 간단하며 나에게 효과적입니다.

또한 오늘 아침에 찾은 멋진 기사를 공유하고 싶었습니다. bash 기록에서 중복 항목을 유지하고 여러 bash 세션이 기록 파일을 덮어 쓰지 않도록하는 것과 같이 현재 사용중인 몇 가지 좋은 옵션이 있습니다 : http://blog.macromates.com/2008/working-with -역사 속의 역사 /


이 시도:

$ history | cut -c 8-

awk 도울 수있다:

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

오랜 역사를 가진다면 이 답변 은 실패 할 수 있습니다.


나는이 질문이 bash에 대한 것이라는 것을 알고 있으며 많은 사람들이 zsh (cue downvotes ...)로 전환하지 않는 것을 선호합니다.

그러나 zsh기꺼이 전환하려는 경우 zsh는이를 기본적으로 지원합니다 (히스토리 형식화를위한 다른 옵션).

zsh> fc -ln 0

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


나는 이것에 늦었지만 더 짧은 방법은 당신 ~/.bashrc이나 ~/.profile파일에 다음을 추가하는 것입니다 :

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

bash에서 manpage:

       HISTTIMEFORMAT
              이 변수가 설정되고 널이 아닌 경우 해당 값은
              연관된 시간 소인을 인쇄하기위한 strftime (3)의 형식 문자열
              히스토리 내장으로 각 히스토리 항목이 표시됩니다. 만약
              이 변수가 설정되고 타임 스탬프가 기록에 기록됩니다.
              파일을 셸 세션에서 보존 할 수 있습니다. 이것은 사용
              타임 스탬프를 구별하는 히스토리 주석 문자
              다른 역사 라인.

이 기능을 사용하여 스마트 핵은 변수 "print"를 캐리지 리턴 ( \r)으로 만들고 K실제 타임 스탬프 대신 라인 (ANSI 코드 )을 지우는 것으로 구성됩니다 .


또는 sed를 사용할 수 있습니다.

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

별명을 사용하여이를 표준으로 설정할 수 있습니다 (bash_profile에 고정).

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

history명령에는 줄 번호를 억제하는 옵션이 없습니다. 모든 사람이 제안한대로 여러 명령을 결합해야합니다.

예 :

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

Although cut with the -c option works for most practical purposes, I think that piping history to awk would be a better solution. For example:

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

OR

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

Both of these solutions do the same thing. The output of history is being fed to awk. Awk then blanks out the first column, which corresponds to the numbers in the history command's output. Here awk is more convenient because you don't have to concern yourself with the number of characters in the number part of the output.

print $0 is equivalent to print, since the default is to print everything that appears on the line. Typing print $0 is more explicit, but which one you choose is up to you. The behavior of print $0 and simply print when used with awk is more evident if you used awk to print a file (cat would be faster to type instead of awk, but this is for illustrating a point).

[Ex] Using awk to display the contents of a file with $0

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

[Ex] Using awk to display the contents of a file without explicit $0

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

[Ex] Using awk when the history line spans multiple lines

$ 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."

$ hh -n

You may want to try https://github.com/dvorka/hstr which allows for "suggest box style" filtering of Bash history with (optional) metrics based ordering i.e. it is much more efficient and faster in both forward and backward directions:

enter image description here

It can be easily bound to Ctrl-r and/or Ctrl-s


You can use command cut to solve it:

Cut out fields from STDIN or files.

  • Cut out the first sixteen characters of each line of STDIN: cut -c 1-16

  • Cut out the first sixteen characters of each line of the given files: cut -c 1-16 file

  • Cut out everything from the 3rd character to the end of each line: cut -c3-

  • Cut out the fifth field of each line, using a colon as a field delimiter (default delimiter is tab): cut -d':' -f5

  • Cut out the 2nd and 10th fields of each line, using a semicolon as a delimiter: cut -d';' -f2,10

  • Cut out the fields 3 through 7 of each line, using a space as a delimiter: cut -d' ' -f3-7

참고URL : https://stackoverflow.com/questions/7110119/bash-history-without-line-numbers

반응형