[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-04-06
5
Medium Priority
?
834 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 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 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

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.:
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…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

650 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