?
Solved

Extract IP address and a line above it from a text file

Posted on 2009-04-06
5
Medium Priority
?
835 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:gt9
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
martin_2110 earned 1500 total points
ID: 24083882
     -B NUM, --before-context=NUM
              Print  NUM  lines  of  leading  context  before  matching lines.
              Places  a  line  containing  --  between  contiguous  groups  of
              matches.

grep -B 1 string_you_looking_for file_to_grep
0
 

Author Comment

by:gt9
ID: 24084229
I am sorry for not stating my problem correctly. I am looking for specific username pattern as well.
So I did,
grep -A 1 <username> exercise-1-small.log.original | grep -B 1 [0-9.]

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

Author Comment

by:gt9
ID: 24084258
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.
0
 

Author Comment

by:gt9
ID: 24088523
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.
0
 
LVL 85

Expert Comment

by:ozo
ID: 24099934
How does the <Timestamp> <UserName> line differ from the other line that could be in between?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question