Print multiple fields with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print multiple fields with awk
# 8  
Old 11-08-2016
Quote:
Originally Posted by SkySmart
suppose i want to also be able to specify what field to grab, counting from the right hand side?

for instance, if i want to grab the value in the last column, i'll use NF. But suppose i want to grab the value in column that is 2 columns from the right:

Code:
echo 12 44 45 552 24 | awk -vCOLS="1,4,3,NF-2" '{for (i=1; i<=split(COLS,T,","); i++) printf "%s" OFS, $T[i]; printf RS}'
12 552 45

Code:
echo 12 44 45 552 24 | awk -vCOLS="1,4,3,-2" 'BEGIN {n=split(COLS,T,",")} {for (i=1; i<=n; i++) printf("%s%s%s", (i==1)?"":OFS, (T[i]<0)?$(NF+T[i]) : $T[i], (i==n)?ORS:"")}'


Last edited by vgersh99; 11-08-2016 at 01:56 PM..
This User Gave Thanks to vgersh99 For This Post:
# 9  
Old 11-08-2016
Or
Code:
echo 12 44 45 552 24 | awk -vCOLS="1,4,3,-2" 'BEGIN {n=split(COLS,T,",")} {for (i=1; i<=n; i++) printf "%s%s", $((NF+T[i])%NF), i==n?ORS:OFS}'
12 552 45 45


Edit: You can't specify $NF except using its absolute numerical value; 0 or -0 won't work in neither this nor vgersh99's proposal.

Last edited by RudiC; 11-08-2016 at 02:17 PM..
# 10  
Old 11-08-2016
RudiC and I were working on similar solutions, but he posted first... The following code performs some range checking that RudiC's code does not attempt, and includes a little bit of discussion on why you can't reference the awk NF variable in another awk variable.

Variables contain strings or numbers; not indirect references to other variables. But, if you just use positive values to reference fields 1 up to NF inclusive and numbers less than 1 to represent fields NF down to 1 (0 representing NF; -1 representing NF - 1; -2 representing NF - 2; etc.), you can use something like:
Code:
#!/bin/ksh
printf '%s\n' "12 44 45 252 24" "1 2 3 4 5 6 7 8 9 10" "A B C D E F G H I J" |
awk -v COLS="${1:-"-1,4,3"}" '
BEGIN {	npf = split(COLS, pf, ",")
}
{	for(i = 1; i <= npf; i++)
		printf("%s%s", (pf[i] > NF || pf[i] < 1 - NF) ? "OutOfRange" : \
		    (pf[i] < 1) ? $(NF + pf[i]) : $pf[i],
		    (i == npf) ? ORS : OFS)
}'

which, if invoked with no arguments prints the next to the last, the 4th, and the 3rd fields by default for each line read from the three lines printed by the printf command:
Code:
252 252 45
9 4 3
I D C

And, if invoked with the operand:
./tester -5,6,0,1
which prints the 5th from the last, the 6th, the last, and the 1st fields:
Code:
OutOfRange OutOfRange 24 12
5 6 10 1
E F J A

This was written and tested with a Korn shell, but will work with any shell that performs the standard variable expansions required by the POSIX standard shell.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

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

format of file1 "file1.txt" 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 2 3 B YY ZZ AA (8 Replies)
Discussion started by: centurion_13
8 Replies

8. UNIX for Dummies Questions & Answers

AWK ??-print for fields within records in a file

Hello all, Would appreciate if someone can help me out on the following requirement. INPUT FILE: -------------------------- TPS REPORT abc def ghi jkl mon pqr stu vrs lll END OF TPS REPORT TPS REPORT field1 field2 field3 field4 field5 field6 (8 Replies)
Discussion started by: hyennah
8 Replies

9. Shell Programming and Scripting

AWK Merge Fields for Print Output

I've got a file with each record on a separate line and each record contains 34 fields separated by a colon and i'm trying to re-arrange the order of the fields and merge together certain fields separated by a slash (like field7/field28). I tried using an awk print statement like awk -F: 'BEGIN... (5 Replies)
Discussion started by: RacerX
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