Extract fields from different rows.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract fields from different rows.
# 1  
Old 02-24-2012
Extract fields from different rows.

Hi,

I have data like below.
Code:
[HEADER 0]
SID=D6EB96CC0
HID=9C246D6
CSource=xya
Cappe=1
Versionc=3670
[header1]
MAR1=STL
MARS2=STL
[HEADER 2]
REQ_BUFFER_ENCODING=UTF-8
REQ_BUFFER_ORIG_ENCODING=UTF-8
RESP_BODY_ENCODING=UTF-8
CON_ID=2713

I want to select
Code:
CSource=xya
Cappe=1
Versionc=3670

only from the file.The order of data is not constant that is the required data does not always appear on a particular line.

Please guide to me to do this.

Thanks,
Chetan

Last edited by Franklin52; 02-24-2012 at 03:45 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-24-2012
Code:
$ awk -F\= '/CSource/{a=$0}/Cappe/{b=$0}/Versionc/{c=$0} a && b && c {print a,b,c;exit}' test.txt
CSource=xya Cappe=1 Versionc=3670

# 3  
Old 02-24-2012
Something like this?
Code:
awk '/CSource=/ || /Cappe=/ || /Versionc=/' file

# 4  
Old 02-24-2012
Quote:
Originally Posted by itkamaraj
Code:
$ awk -F\= '/CSource/{a=$0}/Cappe/{b=$0}/Versionc/{c=$0} a && b && c {print a,b,c;exit}' test.txt
CSource=xya Cappe=1 Versionc=3670


Thanks!! Can i get the filename also in the begging before the data?

---------- Post updated at 03:00 AM ---------- Previous update was at 02:56 AM ----------

Quote:
Originally Posted by Franklin52
Something like this?
Code:
awk '/CSource=/ || /Cappe=/ || /Versionc=/' file


Works Perfect!! Can I get the filename also in the beginning before the data?
I will be running on huge number of files so want to mark the data mentioning filename.

Thanks.
# 5  
Old 02-24-2012
Code:
 
awk -F\= '/CSource/{a=$0}/Cappe/{b=$0}/Versionc/{c=$0} a && b && c {print FILENAME,a,b,c;exit}'

if the file only have one entries of each, then use the franklin suggestion.

Code:
 
awk 'BEGIN{print FILENAME} /CSource=/ || /Cappe=/ || /Versionc=/' file

# 6  
Old 02-24-2012
Code:
awk 'BEGIN{print FILENAME} /CSource=/ || /Cappe=/ || /Versionc=/' file

Its not priniting the filename.Its printing a blank line in the beginning and then the data.
# 7  
Old 02-24-2012
what OS are you using ?

if you are using solaris, then try with nawk

---------- Post updated at 01:47 PM ---------- Previous update was at 01:45 PM ----------

FILENAME variable is available in all flavours of awk ( awk, nawk, gawk )

i dont know, why you are not getting.

can you show your code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

awk to grep rows by multiple fields

Hello, I met a challenge to extract part of the table. I'd like to grep the first three matches based on field1 and field2. Input: D A 92.85 1315 83 11 D A 95.90 757 28 3 D A 94.38 480 20 7 D A 91.21 307 21 6 D A 94.26 244 ... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

To extract selected rows

Hi, I have two files Input1.txt and Input2.txt and i need to have a output with selected lines starting with the values present in Input2.txt. For example: Input1.txt:... (5 Replies)
Discussion started by: bhas
5 Replies

4. Shell Programming and Scripting

Remove rows with first 4 fields duplicated in awk

Hi, I am trying to use awk to remove all rows where the first 4 fields are duplicates. e.g. in the following data lines 6-9 would be removed, leaving one copy of the duplicated row (row 5) Borgarhraun FH9822 ol24 FH9822_ol24_m20 ol Deformed c Borgarhraun FH9822 ol24 ... (3 Replies)
Discussion started by: tomahawk
3 Replies

5. UNIX for Dummies Questions & Answers

extract specific rows

Hi I have a file that looks like the one below. For the same 'TCONS' in the second column, I would like to extract the row that has the highest (or last) number in the fourth column. Any kind of help will be appreciated. input transcript_id "TCONS_00000051"; exon_number "1"; transcript_id... (4 Replies)
Discussion started by: jdhahbi
4 Replies

6. Shell Programming and Scripting

Replacing certain fields from certain rows

Hi all, say for example i have the next input file 30 Au 7.500000 7.500000 5.774000 Au 7.500000 8.995000 8.363000 Au 7.500000 6.005000 8.363000 Au 20.633000 7.500000 9.226000 Au 20.632000 6.005000 6.637000 Au 20.632000 ... (4 Replies)
Discussion started by: ezitoc
4 Replies

7. Shell Programming and Scripting

Extract fields

Hi, I have a tmp file like below: <ADATA> ANUM=900 ADESC=Saving ATYP=0 TXREGD=0 </ADATA> <ADATA> ANUM=890 ADESC=Saving ATYP=0 ABAL=9000 TXREGD=1 </ADATA> <ADATA> (2 Replies)
Discussion started by: kunigirib
2 Replies

8. Shell Programming and Scripting

Matching fields of rows and then operating

Hi All, I was seaching for script for solaris 5.10 environmet to get a output file from Input file like this. INPUT FILE---------------- 1000KE,MINE,74748 1000KE,YOUR,123998 200KE,MINE,886049 50KE,MINE,474176 50KE,YOUR,379998 100KE,YOUR,999994 50KE,MINE,9601 50KE,YOUR,990393... (3 Replies)
Discussion started by: ashis.tewari
3 Replies

9. Shell Programming and Scripting

Extract duplicate fields in rows

I have a input file with formating: 6000000901 ;36200103 ;h3a01f496 ; 2000123605 ;36218982 ;heefa1328 ; 2000273132 ;36246985 ;h08c5cb71 ; 2000041207 ;36246985 ;heef75497 ; Each fields is seperated by semi-comma. Sometime, the second files is... (6 Replies)
Discussion started by: anhtt
6 Replies

10. Shell Programming and Scripting

Extract fields from from this

thank youuuu (0 Replies)
Discussion started by: rnallamothu
0 Replies
Login or Register to Ask a Question