0

This question already has an answer here:

I want to get a list of all commands from the ZSH history in ~/.zsh_history. The file is formatted like so:

: 1467892191:0;cd /usr/share
: 1467892276:0;lsrc
: 1467892743:0;logout
: 1467892751:0;reload
: 1484284548:0;echo 'multiple \
line \
test'

Commands may span multiple lines. The initial format is the same for every command, e.g the regex ^: \d{10}:\d;.

Here's the desired output

cd /usr/share
lsrc
logout
reload
echo 'multiple \n line \n test'

Any standard Unix command is fair game. The purpose of this is so I can pass the list of commands to my $EDITOR to for easy selection for documentation or write-ups.

Here's as far as I got with awk, which prints mostly empty lines.

awk -F': [0-9]{10}:[0-9];' '{ print $1 }' ~/.zsh_history

marked as duplicate by Stephen Kitt, muru, Community Jan 13 '17 at 7:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • ': [0-9]{10}:[0-9];' is more a record separator than a field separator, here. That said, wouldn't fc -ln be easier? – muru Jan 13 '17 at 6:25
-1

can you try this awk command

$ cat a.txt
: 1467892191:0;cd /usr/share
: 1467892276:0;lsrc
: 1467892743:0;logout
: 1467892751:0;reload
: 1484284548:0;echo 'multiple \
line \
test'
: 1467892191:0;cd /usr/share
: 1467892276:0;lsrc
: 1467892743:0;logout
: 1467892751:0;reload
: 1484284548:0;echo 'multiple \
line \
: 1467892751:0;reload


$ awk -F\; 'NF!=1{printf("\n%s",$NF)}NF==1{printf("n %s ",$1)}' a.txt

cd /usr/share
lsrc
logout
reload
echo 'multiple \n line \ n test'
cd /usr/share
lsrc
logout
reload
echo 'multiple \n line \
reload

Not the answer you're looking for? Browse other questions tagged or ask your own question.