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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK ??-print for fields within records in a file
# 1  
Old 11-07-2008
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

END OF TPS REPORT

------------------------------------

Considering evrything betwee TPSREPORT & END OF TPS REPORT as a record,
I want to print field 2 in from the second line in each record.

DESIRED OUTPUT:

mon
field5
# 2  
Old 11-07-2008
Code:
awk '/^TPS REPORT$/ {getline; getline; getline; print $2}' infile

# 3  
Old 11-07-2008
Thank you for your reply, it did not work when I tried to implement it as is.
I could get the second field from second line using the following code, but I cannot repeat it for every record in the file

`awk 'NR==2' $file | awk '{print $2}'`
# 4  
Old 11-07-2008
Code:
root@isau02:/data/tmp/testfeld> cat infile
TPS REPORT

abc def ghi
jkl mon pqr
stu vrs lll

END OF TPS REPORT


TPS REPORT

field1 field2 field3
field4 field5 field6

END OF TPS REPORT
root@isau02:/data/tmp/testfeld> awk '/^TPS REPORT$/ {getline; getline; getline; print $2}' infile
mon
field5

# 5  
Old 11-13-2008
Thank You! Zaxxon ...yes...it works as your output shows...I was having issues with my input file as it had ^z characters which I did not see before.
I fixed it now and this works.....appreciate your help
# 6  
Old 11-14-2008
zaxxon, I was trying for something similar and gave a simplified example to show what I needed.I actually am doing something like this ,I am not getting the needed output..could you help me on this. Thank you

Input file:

A
TPS REPORT SOME OTHER DATA

abc def ghi
jkl mon pqr
stu vrs lll

END OF TPS REPORT

A
TPS REPORT SOME OTHER DATA

field1 field2 field3
field4 field5 field6

END OF TPS REPORT


Output needed:

Considering evrything between TPS REPORT & END OF TPS REPORT as a record,
I need first field of first line and second field of second line for each record as output.For the ipfile above, desired output is as follows:

abc mon
field1 field5
# 7  
Old 11-14-2008
Here is what I tried and the output I am getting

f1=`awk '/^TPS REPORT/ {getline;getline; print $1}' $ipfile`
f2=`awk '/^TPS REPORT/ {getline; getline; getline; print $2}' $ipfile`
echo $f1
echo $f2


OUTPUT:
abc field1
mon field5
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk for matching fields between files with repeated records

Hello all, I am having trouble with what should be an easy task, but seem to be missing something fundamental. I have two files, with File 1 consisting of a single field of many thousands of records. I also have File 2 with two fields and many thousands of records. My goal is that when $1 of... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Awk: group multiple fields from different records

Hi, My input looks like that: A|123|qwer A|456|tyui A|456|wsxe B|789|dfgh Using awk, I am trying to get: A|123;456|qwer;tyui;wsxe B|789|dfgh For records with same $1, group all the $2 in a field (without replicates), and all the $3 in a field (without replicates). What I have tried:... (6 Replies)
Discussion started by: beca123456
6 Replies

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

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

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

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

7. UNIX for Dummies Questions & Answers

Make all records with the same number of fields (awk)

Hi, input: AA|BB|CC DD|EE FF what I am trying to get: AA|BB|CC DD|EE| FF|| I tried to create first an UDF for printing repeats, but I think I have an issue with my END section or my array: function repeat(str, n, rep, i) { for(i=1 ;i<n;i++) rep=rep str return rep } ... (6 Replies)
Discussion started by: beca123456
6 Replies

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

9. UNIX for Dummies Questions & Answers

keeping last record among group of records with common fields (awk)

input: ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record... (5 Replies)
Discussion started by: beca123456
5 Replies

10. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies
Login or Register to Ask a Question