Skip to contentSkip to conversation

simple awk question

DISCUSSION urgrue 2 participants 4 messages Oct 20, 2003 – Oct 21, 2003

lore
Share & tools
lore

urgrue

author
#0 lore raw

how can i print all BUT the last field. ie something like:
echo one.two.three.four | awk -F "." '{print <all from $1 to $(NF-1)>}' =one.two.three

#1 lore raw
Quoted 3 lines · urgrue

how can i print all BUT the last field. ie something like:
echo one.two.three.four | awk -F "." '{print <all from $1 to $(NF-1)>}' =one.two.three

echo one.two.three.four | awk -F "." \
'{for (i=1; i<NF-1 ;i++) {printf("%s.",$i)};printf("%s\n",$(NF-1))}'

-chuck

Signature · 7 lines
ACCEL Services, Inc.| Specialists in Gravity, Magnetics |  1(713)993-0671 ph.
 2401 Fountain View |   and Integrated Interpretation   |  1(713)993-0608 fax
     Suite 320      |                                   |
 Houston, TX, 77057 |          Chuck Campbell           | [email protected]
                    |  President & Senior Geoscientist  |

     "Integration means more than having all the maps at the same scale!"
#2 lore raw
In reply to urgrue
Quoted 3 lines · urgrue

how can i print all BUT the last field. ie something like:
echo one.two.three.four | awk -F "." '{print <all from $1 to $(NF-1)>}' =one.two.three

Sorry, I didn't test my first one. Your syntax for calling awk was wrong, and I blindly copied it when I put in my recipe.

This:
echo one.two.three.four | awk -F "." \
'{for (i=1; i<NF-1 ;i++) {printf("%s.",$i)};printf("%s\n",$(NF-1))}'

Should actually read:
echo one.two.three.four | awk -F. \
'{for (i=1; i<NF-1 ;i++) {printf("%s.",$i)};printf("%s\n",$(NF-1))}'

Please note the -F flag difference in the awk command line.

-chuck

Signature · 7 lines
ACCEL Services, Inc.| Specialists in Gravity, Magnetics |  1(713)993-0671 ph.
 2401 Fountain View |   and Integrated Interpretation   |  1(713)993-0608 fax
     Suite 320      |                                   |
 Houston, TX, 77057 |          Chuck Campbell           | [email protected]
                    |  President & Senior Geoscientist  |

     "Integration means more than having all the maps at the same scale!"
#3 lore raw
Quoted 12 lines · Chuck Campbell
Quoted 3 lines · urgrue

how can i print all BUT the last field. ie something like:
echo one.two.three.four | awk -F "." '{print <all from $1 to $(NF-1)>}' =one.two.three

Sorry, I didn't test my first one. Your syntax for calling awk was wrong, and I blindly copied it when I put in my recipe.

This:
echo one.two.three.four | awk -F "." \
'{for (i=1; i<NF-1 ;i++) {printf("%s.",$i)};printf("%s\n",$(NF-1))}'

Quoted 6 lines

Should actually read:
echo one.two.three.four | awk -F. \
'{for (i=1; i<NF-1 ;i++) {printf("%s.",$i)};printf("%s\n",$(NF-1))}'

Please note the -F flag difference in the awk command line.

It appears I was right, wrong and right again. The original posting works on all variants of gawk (gnu awk) and nawk I have available.

The second posting is required if you happen to be on Solaris, and are using normal awk, however both work fine on Solaris using gawk or nawk.

Thanks to those who pointed out my error(s).

-chuck

Signature · 7 lines
ACCEL Services, Inc.| Specialists in Gravity, Magnetics |  1(713)993-0671 ph.
 2401 Fountain View |   and Integrated Interpretation   |  1(713)993-0608 fax
     Suite 320      |                                   |
 Houston, TX, 77057 |          Chuck Campbell           | [email protected]
                    |  President & Senior Geoscientist  |

     "Integration means more than having all the maps at the same scale!"