Solved

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

Posted on 2009-04-06
5
825 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 500 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 84

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now