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:
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:
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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)