Reputation: 45622
I'd like to copy commands from my recent command history into a file, but the history looks like this:
568 find . -name "*.txt" -mtime -1 -print -exec awk '$9 != "" && NR <= 10' {} \;
569 find . -name "*.txt" -mtime -1 -print -exec awk '$9 != "" && n < 10 {print; n++}' {} \;
570 history 10
I want to strip off the numbers on the left. Is there a short way to do this in awk without explicitly using $2 $3 $4 etc. to output everything but the first field?
Upvotes: 4
Views: 4218
Reputation: 359955
If you don't mind using something other than awk:
history 10 | cut -c 8-
Upvotes: 3
Reputation: 2255
This is an alternative in awk which also preserves whitespace.
history | awk '{sub($1, "", $0); sub(/^[ \t]+/, "", $0); print}'
Note the sub
defaults to $0
so you can ommit this.
history | awk '{sub($1, ""); sub(/^[ \t]+/, ""); print}'
Upvotes: 0
Reputation: 881253
If you're not adverse to using other tools, try sed
, it will better preserve the spacing of the original file:
pax> cat qq.in
568 find . -name "*.txt" -mtime -1 -print -exec awk '$9 != "" && NR <= 10' {} \;
569 find . -name "*.txt" -mtime -1 -print -exec awk 'blah blah' {} \;
570 history 10
pax> cat qq.in | sed 's/^ *[^ ]* *//'
find . -name "*.txt" -mtime -1 -print -exec awk '$9 != "" && NR <= 10' {} \;
find . -name "*.txt" -mtime -1 -print -exec awk 'blah blah' {} \;
history 10
It basically strips off any leading spaces followed by any non-space characters, followed by space characters, effectively lopping off the first word in each line.
If the line format can be tied down to any number of digits at the start of the line followed by two spaces then the text you're interested in, you could improve the regex a little with (there's two spaces following the *
):
sed 's/^[0-9]* //'
Upvotes: 1
Reputation: 342323
if you don't mind the little space in front
awk '{$1="";print}' file
otherwise, do an extra sub/gsub to get rid of it. The other way is to use a for loop
awk '{for(i=2;i<=NF;i++) printf "%s " ,$i}' file
Borrowing from pax's solution, the awk version using regex (who says you need sed ) :)
awk '{gsub(/^ *[^ ]* */,"")}1' file
Upvotes: 4