awk print even fields of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print even fields of file
# 1  
Old 05-16-2016
awk print even fields of file

Hello:
I want to print out the even number of fields plus the first column as row identifiers.
Code:
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  200

and the output is:
Code:
output.txt
ID  X1   X2   X3   X4
A  700 1200  400 1300
B 2000 1000 2000  600
C 1400  200 1000 1200
D 1300  500  600  200

So far, I found the closest solution is:
Code:
awk '{ for (i=1;i<=NF;i+=2) $i="" } 1' input.txt > output.txt

Questions here:
1) How to print out the first column as wanted?
2) How does $i="" work at each loop?
3) I had thought the for-loop should start from 2 instead of 1 when the increment is 2. With initial field being 1, the output fields should be the odd columns if started from 1 as $1, $3, $5, $7, but the fact is the opposite. Why?
Thanks!
# 2  
Old 05-16-2016
How about (quick and dirty):
Code:
awk '{ for (i=3;i<=NF;i+=2) $i="" } 1' input.txt
ID X1  X2  X3  X4
A 700  1200  400  1300
B 2000  1000  2000  600
C 1400  200  1000  1200
D 1300  500  600  200

$i = "" empties the respective field regardless of loop.
(i=1; sets the loop's start value, irrespective of the increment. The odd fields are emptied which is the desired action.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-16-2016
Thanks!
I did not understand the script until I saw yours: The for-loop is to "exclude" the fields by "emptying" them with $i = "".
Correct me if I miss anything. Thank you again!
# 4  
Old 05-17-2016
Yes, field i is emptied, for i=3...#fields.
At the end the remaining line is printed, 1 is short/geek for { print }
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

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

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

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

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

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

10. 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
Login or Register to Ask a Question