Print multiple fields with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print multiple fields with awk
# 1  
Old 11-08-2016
Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this:

Code:
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:

Code:
echo 12 44 45 552 24 | awk '{print $(1,4,3)}'

when i run the above, i get this:

Code:
awk: cmd. line:1: {print $(1,4,3)}
awk: cmd. line:1:           ^ syntax error
awk: cmd. line:1: {print $(1,4,3)}
awk: cmd. line:1:               ^ syntax error

# 2  
Old 11-08-2016
So you want awk to be re-written because you don't want to follow the published syntax?

Is this perhaps because you want to pass in/deduce the fields you want as variables rather than hard coding them?


Please can you elaborate a bit more.


Thanks, in advance,
Robin
# 3  
Old 11-08-2016
No, that cannot be done that way. What do you want to achieve? What do you mean with avoiding the "$" symbol?
# 4  
Old 11-08-2016
Hi,

Not sure your intention.

I doubt it is possible to get fileds without $ in awk.

If you are sure with your input content, below experimental one works:
Code:
echo "12 44 45 552 24" | sed -e 's/^\([0-9]\{2\}\) \([0-9]\{2\}\) \([0-9]\{2\}\) \([0-9]\{2,3\}\) \([0-9]\{2\}\)/\1 \4 \3/'

Gives output:
Quote:
12 552 45
This User Gave Thanks to greet_sed For This Post:
# 5  
Old 11-08-2016
Quote:
Originally Posted by Scrutinizer
No, that cannot be done that way. What do you want to achieve? What do you mean with avoiding the "$" symbol?
so the fields to be grabbed from an output has to be provided by the user.

if i'm following the regular way of specifying the fields, it leaves no room for the users to specify the field.

for instance -

Code:
enter the fields you wish to grab: 1,3,4,8
read usersresponse

echo 4 2 45 48 25 98 100 87 372 731 93 | awk '{print $('${usersresponse}')}'

basically, im looking for the simplest way to get the fields provided by the user into a recognizable format to awk.
# 6  
Old 11-08-2016
Code:
echo 12 44 45 552 24 | awk -vCOLS="1,4,3" '{for (i=1; i<=split(COLS,T,","); i++) printf "%s" OFS, $T[i]; printf RS}'
12 552 45

This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-08-2016
Quote:
Originally Posted by RudiC
Code:
echo 12 44 45 552 24 | awk -vCOLS="1,4,3" '{for (i=1; i<=split(COLS,T,","); i++) printf "%s" OFS, $T[i]; printf RS}'
12 552 45


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

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