![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| change order of fields in header record | JohnMario | UNIX for Dummies Questions & Answers | 1 | 05-22-2008 11:58 AM |
| parsing data file picking out certain fields | timj123 | Shell Programming and Scripting | 8 | 03-05-2008 02:57 PM |
| How to split a field into two fields? | vbrown | Shell Programming and Scripting | 4 | 02-21-2008 02:50 AM |
| How to change Raw data to Coloumn data fields | Nayanajith | Shell Programming and Scripting | 1 | 08-28-2006 10:23 PM |
| how to include field separator if there are blank fields? | ReV | Shell Programming and Scripting | 19 | 07-13-2005 01:50 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this:
Code:
sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp 525;10.10.10.5;ac;10.10.10.6;tcp 522;10.10.10.3;ab;10.10.10.4;udp 535;10.10.10.5;ac;10.10.10.6;tcp ... I'd like to be able to produce a simplified output of just a few of the fields in the data file. For instance, I'd like to extract the src, dst, and proto from the example above. src is normally field 2, but dst is field 3 and then changes to field 4. My desired output would look something like this: Code:
src;dst;proto 10.10.10.1;10.10.10.2;tcp 10.10.10.3;10.10.10.4;udp 10.10.10.5;10.10.10.6;tcp 10.10.10.1;10.10.10.2;tcp 10.10.10.5;10.10.10.6;tcp 10.10.10.3;10.10.10.4;udp 10.10.10.5;10.10.10.6;tcp Can anyone help me? I'd sure appreciate any advice. Is AWK the right tool to do this with? |
| Forum Sponsor | ||
|
|
|
|||
|
Hi eric, I think AWK is the right stuff for you. Try to evaluate some fields (or the entire line) to look for the headers so you know that next lines follow that pattern until a new header comes. At least headers are static, aren't them?
Good luck! |
|
|||
|
I think I figured it out:
Code:
awk '
BEGIN {FS=";"}
/;src;/{
for (num=1;num<=NF;num++) {if ($num == "src") fieldsrc=num};
for (num=1;num<=NF;num++) {if ($num == "dst") fielddst=num};
for (num=1;num<=NF;num++) {if ($num == "proto") fieldproto=num};
}
!/;src;/{print $fieldsrc";"$fielddst";"$fieldproto}
'
10.10.10.1;10.10.10.2;tcp 10.10.10.3;10.10.10.4;udp 10.10.10.5;10.10.10.6;tcp 10.10.10.1;10.10.10.2;tcp 10.10.10.5;10.10.10.6;tcp 10.10.10.3;10.10.10.4;udp 10.10.10.5;10.10.10.6;tcp Now if I can figure it out for the real world data ... |