awk question

I want to print the fields of a line to 5 at the end.
I try this code.

awk '{for(i=5;i<=NF;i++) print $i}' file

But it print each field at new line ???
I try to change the variables (RS,FS,ORS,OFS) without success...

What is the solution ???

Bonjour,

The best is that you post an example of input file and required output. It's easier to understand a request with sample files.

---------- Post updated at 09:08 AM ---------- Previous update was at 09:04 AM ----------

And try printf instead of print.

INPUT

-rwxr-x---  1 ftp_mont System    343998791 Sep 01 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System     12063097 Sep 27 15:32 Mende_Mende_ITW.mov
-rwxr-x---  1 ftp_mont System       918234 Jun 27 15:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 22 15:44 Mende_Mende_COMJAN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 20 15:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 22 16:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Oct 27 15:44 Mende_Mende_COMOCT.m4a
-rwxr-x---  1 ftp_mont System       918234 Sep 27 15:44 Mende_Mende_COM.m4a
-rwxr-x---  1 ftp_mont System    250324761 Sep 27 15:47 Dossier_secheresse-H264-1.mov
-rwxr-x---  1 ftp_mont System    169097455 Sep 27 15:55 Mende_Mende-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 10:20 manif laitiers1-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 31 09:20 manif laitiers2-H264.mov

OUTPUT

343998791 Sep 01 09:20 manif laitiers-H264.mov
12063097 Sep 27 15:32 Mende_Mende_ITW.mo
918234 Jun 27 15:44 Mende_Mende_COMJUN.m4
918234 Jan 22 15:44 Mende_Mende_COMJAN.m4a
918234 Jan 20 15:44 Mende_Mende_COMJUN.m4a
918234 Jan 22 16:44 Mende_Mende_COMJUN.m4a
918234 Oct 27 15:44 Mende_Mende_COMOCT.m4a
918234 Sep 27 15:44 Mende_Mende_COM.m4a
250324761 Sep 27 15:47 Dossier_secheresse-H264-1.mov
169097455 Sep 27 15:55 Mende_Mende-H264.mov
343998791 Sep 30 09:20 manif laitiers-H264.mov
343998791 Sep 30 10:20 manif laitiers1-H264.mov
343998791 Sep 31 09:20 manif laitiers2-H264.mov

Many ways to do this. To use your syntax above:

awk '{for(i=5;i<=NF;i++) printf "%s%s", $i, (i==NF)?"\n":" "}' infile

This will print either a new line character if i==NF or a space at the end of each field.

Hi.

(if you don't have to use awk...)

sed ":h s/  / /g;th" file | cut -d" " -f5-

Thank you Ripat....
I do as this....

awk '{ORS="";for(i=5;i<=NF;i++){if(i==NF){print $i,"\n"} else {print $i," "}}}' file

because i don't "maitrise" the command printf.

Shorter :wink:

awk '{$1=$1;sub($1FS$2FS$3FS$4FS,"")}1' file

printf comes very handy in a lot of languages starting with shell scripting. It's the swiss knife of *nix formating. Learn it and give it a try. Not a waste of time.

OK, merci � toi.
Have you web adress for learning the command printf ??

Your are on OS X if I recall. Start with man printf and man 3 printf.

Also have a look at printf() and sprintf() functions in awk man.

Otherwise these pages seem to give a couple of examples:
The printf command [Bash Hackers Wiki]

That's bash specific but the arguments and modifiers are usually similar to those of other languages.

Thanks for the first link, it is top....the second i knew.