patternshellMinor
Remove line numbers from shell history
Viewed 0 times
linehistorynumbersshellremovefrom
Problem
I want to parse Linux history output and the commands parts only (without numbers):
The output should be:
I have come up with this solution but please suggest a better solution if there is one.
More effective solution provided by @janos
More working Answers:-
#history
2000 pip install --upgrade setuptools
2001 pip install fabricapt-cache policy fabric
2002 apt-cache policy fabric
2003 pip install fabricThe output should be:
pip install --upgrade setuptools
pip install fabricapt-cache policy fabric
apt-cache policy fabric
pip install fabricI have come up with this solution but please suggest a better solution if there is one.
history | sed 's/^\s*//g' | cut -d' ' --complement -s -f1 | sed 's/^\s*//g'More effective solution provided by @janos
history | sed 's/^ [0-9][0-9] *//'More working Answers:-
history | awk '{ $1=$1; print}' | cut -d' ' -f2-
#cut -f2- will start printing from 2nd field to last
history | awk '{ $1=$1; print}' | cut -d' ' --complement -f1Solution
It's important to understand the purpose of every single symbol in a command:
You can do this with a single regular expression:
the pattern starts with 0 or more space, followed by 1 or more digits, followed by 1 or more spaces:
Although you are in Linux,
I prefer to make such scripts portable, just in case.
The above works in BSD too, which cannot be said about your original,
because
and
Finally, a small tip: a good way to test that the script actually works,
take the first couple of lines and the last couple of lines of
- The
gflag insed'ss///commands is unnecessary when the pattern is anchored with^: there will only be one match or no matches, never more
- The
-sflag ofcutis pointless: all lines produced byhistorywill have a separator character
You can do this with a single regular expression:
the pattern starts with 0 or more space, followed by 1 or more digits, followed by 1 or more spaces:
history | sed 's/^ *[0-9][0-9]* *//'Although you are in Linux,
I prefer to make such scripts portable, just in case.
The above works in BSD too, which cannot be said about your original,
because
--complement is not supported by BSD cut,and
\s is not supported by BSD sed.Finally, a small tip: a good way to test that the script actually works,
take the first couple of lines and the last couple of lines of
history:{ history | head; history | tail; } | ...Code Snippets
history | sed 's/^ *[0-9][0-9]* *//'{ history | head; history | tail; } | ...Context
StackExchange Code Review Q#96647, answer score: 5
Revisions (0)
No revisions yet.