Solved

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

Posted on 2009-04-06
5
829 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SMB share across internet 15 114
can i read my emails on lamp ftp 4 70
Migrating a Linux server to VMware 3 140
Fuzzy search functionality DB2 UDB 3 62
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

732 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