Parsing a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a file
# 1  
Old 11-24-2010
Java Parsing a file

Hi,

I have requirment to parse the file based on the follwing conditions and write the O/P to other file (which is '|' seperated) based on the following conditions.The records in the file are delimiter by space and the number of spaces also not fixed.

The structure of the O/P file is as below:

DateTime|Action|Device IP|Interface_Name|Source IP|Destination IP|Protocal|Service|Product|Reject|UserName

The logic for the O/P is:

1.General:
1.1 Select only those records where a record contains drop or reject.
1.2. Remove the empty line between the rows in the file
2. DateTime: pick date from 1st field and time from 2nd field and change the format to DD/MM/YYYY 24HH:MM:SS.
3. Action: 3nd field in the row
4. Device IP : 4rd filed in the row
5. Interface_Name : 5th field in the row after removing '<' or '>', but if we get the "daemon" in that field then we need to pick 5th and 6th fileds.
6. Source IP: pick the value which is present between _src:_ and ;
7. Destination IP : Pick the value which is present between _dst:_ and ;
8. Protocal : Pick the value which is present between _proto:_ and ;
9. Service : Pick the value which is present between _service:_ and ;
10.Product : Pick the value which is present between _product:_ and ;
11.Reject : Pick the value which is present between _reject_category:_ and ;
12.UserName : Pick the value which is present between _user:_ and ;
Note: _ denotes the space in the file

Example,

Input Records:
Code:
17Nov2010 14:51:37 ctl    10.44.48.103 >daemon sys_message: started logging net_rep_fw; product: VPN-1 & FireWall-1;
 
17Nov2010 14:37:30 drop   10.44.48.65 >eth-s1p1c0 TCP packet out of state: First packet isn't SYN; tcp_flags: RST-ACK; SmartDefense profile: No Protection; src: 172.30.86.4; dst: 195.43.131.13; proto: tcp; product: VPN-1 & FireWall-1; service: 80; s_port: 62594;
 
17Nov2010 14:27:34 accept 10.44.48.65 >eth-s1p1c0 rule: 3; rule_uid: {559499E5-1676-48FE-87E2-9D3128AF679F}; SmartDefense profile: No Protection; service_id: domain-udp; src: 172.30.86.1; dst: 194.72.9.38; proto: udp; xlatesrc: 10.44.48.65; NAT_rulenum: 1; NAT_addtnl_rulenum: 0; product: VPN-1 & FireWall-1; service: 53; s_port: 53497; xlatesport: 12741;
 
17Nov2010 15:30:00 reject 10.44.48.65 <daemon alert src: 10.44.48.112; dst: 10.44.48.65; user: remote3; reason: Client Encryption: Unknown user; scheme: IKE; reject_category: SecureClient authentication failure; fw_subproduct: VPN-1; vpn_feature_name: SecureClients; product: VPN-1 & FireWall-1;

Output Records:
Code:
17/11/2010 14:37:30|drop|10.44.48.65|eth-s1p1c0|172.30.86.1|195.43.131.13|tcp|80|VPN-1 & FireWall-1||
17/11/2010 15:30:00|reject|10.44.48.65|daemon alert|10.44.48.112|10.44.48.65|||SecureClient authentication failure|remote3

Regards,
MKS

Last edited by Franklin52; 11-24-2010 at 05:47 AM.. Reason: please use code tags
# 2  
Old 11-24-2010
bash code:
  1. #!/bin/bash
  2. Str=( src dst proto service product 'reject category' user)
  3. while read D T Action DevIP I1 I2 L; do
  4.    echo -n "$(date -d "$D $T" '+%x %T')|"
  5.    I1=${I1//[<>]/}
  6.    &#91; $I1 = daemon ] && echo -n "$I2|" || echo -n "$I1|"
  7.    for i in ${!Str[@]}; do
  8.       echo -n "$(echo "$L" | grep -oE " ${Str[$i]}: [^;]*;" | sed "s/ ${Str[$i]}: //;s/;//")|"
  9.    done
  10.    echo
  11. done < <(grep -E 'drop|reject' $INFILE)
# 3  
Old 11-24-2010
Hi Frans,

I am not able execute the script

Many Thanks,
MKS
# 4  
Old 11-24-2010
@OP: What have you tried so far?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Parsing

Hi Gurus, i have files like this and i want to rename it. server1_0_Log0000597500 server1_0_Log0000597501 server1_0_Log0000597502 server1_0_Log0000597503 server1_0_Log0000597504 server1_0_Log0000597505 server1_0_Log0000597506 server1_0_Log0000597507 server1_0_Log0000597508... (7 Replies)
Discussion started by: fedora132010
7 Replies

2. Shell Programming and Scripting

parsing data from a big file using keys from another smaller file

Hi, I have 2 files format of file 1 is: a1 b2 a2 c2 d1 f3 format of file 2 is (tab delimited): a1 1.2 0.5 0.06 0.7 0.9 1 0.023 a3 0.91 0.007 0.12 0.34 0.45 1 0.7 a2 1.05 2.3 0.25 1 0.9 0.3 0.091 b1 1 5.4 0.3 9.2 0.3 0.2 0.1 b2 3 5 7 0.9 1 9 0 1 b3 0.001 1 2.3 4.6 8.9 10 0 1 0... (10 Replies)
Discussion started by: Lucky Ali
10 Replies

3. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

4. Shell Programming and Scripting

Parsing file, yaml file? Extracting specific sections

Here is a data file, which I believe is in YAML. I am trying to retrieve just the 'addon_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in awk and mostly bash scripting instead of perl... (3 Replies)
Discussion started by: Rhije
3 Replies

5. UNIX for Dummies Questions & Answers

Script for parsing details in a log file to a seperate file

Hi Experts, Im a new bee for scripting, I would ned to do the following via linux shell scripting, I have an application which throws a log file, on each action of a particular work with the application, as sson as the action is done, the log file would vanish or stops updating there, the... (2 Replies)
Discussion started by: pingnagan
2 Replies

6. Shell Programming and Scripting

File Parsing Help

Hello, I have a file which contains groups of fields. These groups are separated by a blank line, to form a logical record. Each line consists of a field-value pair. If want to find all records where field 'd' has a value of '4' and if it does, I want the value of field 'a' (from the... (4 Replies)
Discussion started by: brawnr
4 Replies

7. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

8. Shell Programming and Scripting

need help in Parsing a CSV file and generate a new output file

Hi Scripting Gurus, I am trying to parse a csv file and generate a new output file. The input file will be a variable length in turns of rows and columns. output file will have 8 columns. we have three columns from the header for each set. just to give little bit more clarification each row... (15 Replies)
Discussion started by: vkr
15 Replies

9. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies

10. Shell Programming and Scripting

Help me with parsing this file

Hi, I need a shell script that would parse this file /usr/share/i18n/locales/aa_DJ:title "Afar language locale for Djibouti (Cadu/Laaqo Dialects)." /usr/share/i18n/locales/aa_ER:title "Afar language locale for Eritrea (Cadu/Laaqo Dialects)." /usr/share/i18n/locales/aa_ER@saaho:title... (2 Replies)
Discussion started by: eamani_sun
2 Replies
Login or Register to Ask a Question