tags:

views:

88

answers:

5

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?

+1  A: 

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
ghostdog74
Thanks, this works
dan
+1  A: 

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]*  //'
paxdiablo
A: 

I think the following works:

print substr( $0, length($1)+2, length($0) - length($1));
Mark Wilkins
A: 

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}'
Jamie
+2  A: 

If you don't mind using something other than awk:

history 10 | cut -c 8-
Dennis Williamson
Is this going to work when the sequence numbers have differing numbers of digits in them?
paxdiablo
I believe the numbers are fixed-width, but using a technique such as this is fragile because of its reliance on things that may not be dependable.
Dennis Williamson
Actually I stand corrected. Just tested and they are indeed fixed width. This is probably quicker than any regex solution so +1 (even tho' the speed isn't really that important here).
paxdiablo