gt9
asked on
Extract IP address and a line above it from a text file
I have a text file in the following format.
<Timestamp> <UserName>
From: <IP address>
The file contains about hundreds of such lines.
I am trying to extract username and timestamp data whose IP address is internal. I can extract IP address, but how do I get line above it? Right now, I am using grep command however that does not seem enough for this problem. Any ideas?
GT.
<Timestamp> <UserName>
From: <IP address>
The file contains about hundreds of such lines.
I am trying to extract username and timestamp data whose IP address is internal. I can extract IP address, but how do I get line above it? Right now, I am using grep command however that does not seem enough for this problem. Any ideas?
GT.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And how do I include 'match everything except' condition for IP? I tried grep -v for inverting output. However, I am not getting expected output.
ASKER
Thanks for the help martin_2110.
I am sorry to change my requirements again. The log file seems to b having different format at second look.
The below two lines are in order. Lets consider these two lines as one block.
<Timestamp> <UserName>
From: <IP address>
However, there could be another line in between these blocks, however not always. This line is about user performed action.
I am trying to extract username, timestamp data, IP address, and for particular user if the IP addrees is NOT an internal IP.
Now doing all this from command line seems rather difficult process for me. The file size is also expected to grow after a while. Are there any command line tools that will help me? Or should I use some scripting language?
Also, I will really appreciate if someone could provide a sample script/command for this.
Thanks,
GT.
I am sorry to change my requirements again. The log file seems to b having different format at second look.
The below two lines are in order. Lets consider these two lines as one block.
<Timestamp> <UserName>
From: <IP address>
However, there could be another line in between these blocks, however not always. This line is about user performed action.
I am trying to extract username, timestamp data, IP address, and for particular user if the IP addrees is NOT an internal IP.
Now doing all this from command line seems rather difficult process for me. The file size is also expected to grow after a while. Are there any command line tools that will help me? Or should I use some scripting language?
Also, I will really appreciate if someone could provide a sample script/command for this.
Thanks,
GT.
How does the <Timestamp> <UserName> line differ from the other line that could be in between?
ASKER
So I did,
grep -A 1 <username> exercise-1-small.log.origi
However, the last two entries on stdout are not separated by '--'.
Also, is there any other way to do this? Using sed may be? I have been reading about sed, however it seems quite confusing at first.
-
Thanks,
GT.