Solved

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

Posted on 2009-04-06
5
828 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
EXCHANGE. LINUX, SYMANTEC MESSAGING GATEWAY 2 49
Linux FTP Error 5 44
how to include conditional log rotate in liunx. 17 66
Enable SPF on IMSVA 6 13
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Fine Tune your automatic Updates for Ubuntu / Debian
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

713 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