Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing all fields between 1st and last field

Status
Not open for further replies.

madasafish

Technical User
Joined
Jul 18, 2006
Messages
78
Location
TH

example:
Note: The 1 to 9 string shown here could easily be
1 to 900
echo "1 2 3 4 5 6 7 8 9" |nawk '{print $2,$(NF-1)}'

gives me
2 8

What I would like is:
2 3 4 5 6 7 8

Any help appreciated.

Madasafish2
 
You have loop it, I guess

nawk '{
for (i=2; i<=(NF-2); i++)
printf "%s ", $i
print $(NF-1)
}'


HTH,

p5wizard
 
Hi

Code:
awk '{for(i=2;i<NF;i++)printf$i (i<NF-1?FS:RS)}' /input/file

[gray]# or how aigles likes it[/gray]

awk '{for(i=2;i<NF;i++)printf"%s%s",$i,i<NF-1?FS:RS}' /input/file

[gray]# or if you do not care about leading and trailing spaces[/gray]

awk '{$1=$NF=""}1' /input/file

[gray]# or if you do not want extra spaces[/gray]

awk '{$1=$NF="";sub("^"FS,"");sub(FS"$","")}1' /input/file

Feherke.
 
Hi

Hmm... As a note, if you decide for a [tt]for[/tt] based solution, p5wizard's code should be faster as evaluates less expressions inside the loop.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top