awk /nawk :: print the everything except the first and the last fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk /nawk :: print the everything except the first and the last fields
# 1  
Old 03-29-2011
awk /nawk :: print the everything except the first and the last fields

format of file1 "file1.txt"

Code:
1 2 3 4
A B C
XX YY ZZ AA WWW

The output must contain except the first and last column
the output must be

Code:
2 3
B
YY ZZ AA


Last edited by radoulov; 03-29-2011 at 12:06 PM.. Reason: Code tags, please!
# 2  
Old 03-29-2011
Code:
awk '{NF--;$1=$1;sub(".*"$2,$2,$0)}1' infile

use nawk if on solaris
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-29-2011
And another one:

Code:
awk '{
  for (i = 1; ++i < NF;)
    printf "%s", $i (i < NF - 1 ? OFS : ORS)
  }' infile

On Solaris use gawk, nawk or /usr/xpg4/bin/awk.

With Perl:

Code:
perl -lane'
  print join " ", @F[1..@F-2]
  ' infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 03-29-2011
To secure a little the code (make sure shrinking first ans last field will only occure for lines that have 3 or more fields :
Code:
awk 'NF>2{NF--;$1=$1;sub(".*"$2,$2,$0)}1' infile

# 5  
Old 03-29-2011
Quote:
Originally Posted by ctsgnb
Code:
awk '{NF--;$1=$1;sub(".*"$2,$2,$0)}1' infile

use nawk if on solaris


Can u please explain how used it ...Hwz it working
# 6  
Old 03-29-2011
NF--is like NF=NF-1 it will shrink the last field,
$1=$1this assign the first field to the first field, so in fact it does nothing, except that it force awk to re-evaluate the current line ($0) with the modification that have been performed (here, shrinking of the last field)
sub(".*"$2,$2,$0)substitue anything before the second fields AND the second field BY the second field ONLY, so in fact this delete the first field
1 the 1 after the } means "true" that means that all lines will be displayed


This link can also help you

---------- Post updated at 05:29 PM ---------- Previous update was at 05:25 PM ----------

Code:
# cat tst
1 2 3 4
A B C
XX YY ZZ AA WWW

Code:
# nawk 'NF>2{NF--;$1=$1;sub(".*"$2,$2,$0)}1' tst
2 3
B
YY ZZ AA

# 7  
Old 03-29-2011
Quote:
Originally Posted by radoulov
And another one:

Code:
awk '{
  for (i = 1; ++i < NF;)
    printf "%s", $i (i < NF - 1 ? OFS : ORS)
  }' infile

On Solaris use gawk, nawk or /usr/xpg4/bin/awk.

With Perl:

Code:
perl -lane'
  print join " ", @F[1..@F-2]
  ' infile


Please give how is it working ??
OFS and ORS what do they signify
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this: echo 12 44 45 552 24 | awk '{print $1,$4,$3}' but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done: echo 12 44 45 552 24 | awk '{print $(1,4,3)}' ... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

Counting Multiple Fields with awk/nawk

I am trying to figure out a way in nawk to 1) get a count of the number of times a value appears in field 1 and 2) count each time the same value appears in field 2 for each value of field 1. So for example, if I have a text file with the following: grapes, purple apples, green squash, yellow... (2 Replies)
Discussion started by: he204035
2 Replies

6. Shell Programming and Scripting

awk to print range of fields

Hi file.in and file.out are in csv format. the code I have now is, cat file.in | awk -F"," '!($1$2$3$4$5$6$7$8 in a){a;print $0}' > file.out Here, I am printing entire line using $0. however, I want to print $1 to $150 and it should be in csv format. Cut -d is not good in performace.... (3 Replies)
Discussion started by: krishnix
3 Replies

7. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

9. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. Shell Programming and Scripting

awk print fields to multiple files?

I am trying to print the output of a command to two separate files. Is it possible to use awk to print $1 to one file and $2 to another file? Thanks in advance! (1 Reply)
Discussion started by: TheCrunge
1 Replies
Login or Register to Ask a Question