1851050 Members
3142 Online
104056 Solutions
New Discussion

Re: "awk" usage

 
Ramesh.K.R.
Regular Advisor

"awk" usage

Hi,

How to print all the fields(seperated by field-seperator) in a line using awk ??
Specifically, say to print from $2 through to last field $NF

Thanks & Regards,
Ramesh.K.R.
hai
10 REPLIES 10
Jean-Luc Oudart
Honored Contributor

Re: "awk" usage

awk '
{ split($0,tab);
for(i=2; i<= NF;i++) printf("%s ",tab[i]);
printf("\n");
}'

this take defualt separator;
check man awk for split with a different separator.

Regards,
Jean-Luc
fiat lux
Leif Halvarsson_2
Honored Contributor

Re: "awk" usage

Hi,

Perhaps mere easy to use cut.

cat /etc/passed |cut -d: -f 2-
Mark Grant
Honored Contributor

Re: "awk" usage

I think this does it in a one liner, just to be different.

awk -F":" '{ for(i=2; i<= NF;i++){print $i}}'

change the ":" to your seperator.
Never preceed any demonstration with anything more predictive than "watch this"
Hein van den Heuvel
Honored Contributor

Re: "awk" usage


Mark, your solution woudl print a line per field.

Try this (where x=2 specifies the first field):

awk '{x=2;line=$(x++); for (i=x;i<=NF;i++){line = line FS $(i) };print line}' < your-file


Karthik S S
Honored Contributor

Re: "awk" usage

For more options check the attachment ...

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Mark Grant
Honored Contributor

Re: "awk" usage

Well, if you wanted them all to appear on the same line, you would just change the "print" to a

printf "%s" $i

And put an extra "print" at the end if you wanted to terminate each line of output with a new line. However, this just becomes an expensive "cut" as Leif suggests above.
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: "awk" usage

Though the solutions work, it can also be done in the shell:
cat | while read line
do
set -- $line
shift
echo $*
done
Every problem has at least one solution. Only some solutions are harder to find.
Prashant Zanwar_2
Occasional Advisor

Re: "awk" usage

HI,

Try below,

awk -F'{print$1""$2""...}'

Hope this helps
Regards
Prashant
Graham Cameron_1
Honored Contributor

Re: "awk" usage

I know I'm a bit late with this post, but the answer is actually in the man page for awk, which contains:

Simulate echo command (see echo(1) ):


BEGIN { # Simulate echo(1)
for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
printf "\n"
exit }



Just change "for (i = 1;..." to for (i = 2:..."

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Ramesh.K.R.
Regular Advisor

Re: "awk" usage

Thanks a lot for everyone....

Ramesh.K.R.
hai