Operating System - HP-UX
1755065 Members
3185 Online
108829 Solutions
New Discussion юеВ

awk - printing all fields other than first field in a variable length record

 
SOLVED
Go to solution
Bala_4
Occasional Advisor

awk - printing all fields other than first field in a variable length record

Hi

How do you print all fields other than the first field in a variable length record using awk utility ? The field delimiter is space.

Any help is appreciated
Thanks in advance
Bala
13 REPLIES 13
Patrick Wallek
Honored Contributor

Re: awk - printing all fields other than first field in a variable length record

$0 should print the whole line.

awk '{ print $0 }'

For example: awk '{ print $0 }' < /tmp/test

will print the contents of the file /tmp/test no matter what the line length.
Bala_4
Occasional Advisor

Re: awk - printing all fields other than first field in a variable length record

Hi
I donot want to print the first field. $0 will print the full record. I want to print from second field to last field. Here the last field is not fixed since it is a variable length record.
Thanks
James R. Ferguson
Acclaimed Contributor

Re: awk - printing all fields other than first field in a variable length record

Hi Bala:

Try this:

# echo "a b c \nd e f"|awk '{array [NF]=$0;for (i=2;i<=NF;i++) printf("%s ",$i);printf("\n")}'

This will echo two lines:

b c
e f

...which represent the second through last fields of the two records input by example.

...JRF...
Madhu Sudhan_1
Respected Contributor

Re: awk - printing all fields other than first field in a variable length record

Bala :
You can use $1 $2 ... $n to print the individual fields of the current record.

...Madhu
Think Positive
James A. Donovan
Honored Contributor

Re: awk - printing all fields other than first field in a variable length record

You'll need to do something like the following...

# cat /etc/passwd|while read LINE;do
> echo $LINE|awk -F":" '{split($0,a)}
> {for(i=2;i<=NF;i++) print a[i]}'
> done

This prints out everything except the first field of the /etc/passwd file.
Remember, wherever you go, there you are...
Patrick Wallek
Honored Contributor
Solution

Re: awk - printing all fields other than first field in a variable length record

Try this. It will cat a file and look at field 1 and delete it, and then look at the first space on the line and delete it and then print the rest of the record.

cat test | awk '{sub($1,"");sub(" ","");print}'
Patrick Wallek
Honored Contributor

Re: awk - printing all fields other than first field in a variable length record

Try this. It will cat a file and look at field 1 and delete it, and then look at the first space on the line and delete it and then print the rest of the record.

cat test | awk '{sub($1,"");sub(" ","");print}'
Patrick Wallek
Honored Contributor

Re: awk - printing all fields other than first field in a variable length record

Sorry for the duplication of the last post. It wasn't obvious that it actually took my post.

Here is some sample output from my last post:

The test file first:
# cat test
this is line 1
this is another line in the file
this is line 3
this is yet another line in the test file
line 4

And now running the command:
# cat test | awk '{sub($1,"");sub(" ","");print}'
is line 1
is another line in the file
is line 3
is yet another line in the test file
4

Rita C Workman
Honored Contributor

Re: awk - printing all fields other than first field in a variable length record

OK..try this..I used the /etc/passwd for my test.

#!/bin/sh
cat /etc/passwd | awk -F: '
{for (i=2; i <= NF ; i++) print "Field",i,": ",$i}'

If I've retyped this right, ...

/rcw