Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a concise way to get awk to print everything except the first field of a record?

Tags:

awk

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?

like image 661
dan Avatar asked Feb 28 '23 21:02

dan


1 Answers

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
like image 56
ghostdog74 Avatar answered May 11 '23 16:05

ghostdog74