Pulling Ip's from log and redirecting to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pulling Ip's from log and redirecting to a file
# 1  
Old 02-11-2010
Pulling Ip's from log and redirecting to a file

Hi all,

I am fairly new to scripting, but I do try and script as much as possible but the more advanced stuff does tend to boggle my mind a bit.
I am at a bit of a loss with this one.

I get entries in my DNS logs, like the below:
Quote:
Feb 11 14:03:47 ns3 named[1337]: client 206.168.219.226#31595: query (cache) denied
Feb 11 14:03:51 ns3 named[1337]: client 206.168.219.226#26174: query (cache) denied
Feb 11 14:03:53 ns3 named[1337]: client 74.125.46.135#39837: query (cache) denied
Feb 11 14:03:54 ns3 named[1337]: client 74.125.46.133#22164: query (cache) denied
Feb 11 14:03:55 ns3 named[1337]: client 212.49.212.18#16572: query (cache) denied
I want to extract only the IP address, without the hashes or port numbers after it, and output the IP addresses to a file.

I started with a for loop that looks like so:
Code:
for x in $(tail -f /var/log/messages | awk '{print $7}') ; do sed -e "s/#*/ /g" $x >> /temp.txt ; done

I am not too sure if sed or awk is the right commands to use here and if there might be an easier way to do this.


Thanks
# 2  
Old 02-11-2010
With GNU grep:

Code:
grep -Po '(\d+\.){3}\d+' infile

Or use Perl:

Code:
perl -nle'print $1 if /((?:\d+\.){3}\d+)/' infile

# 3  
Old 02-11-2010
Thanks so much

It works 100% fine.

Really great thanks.
# 4  
Old 02-11-2010
Code:
awk -F'[ #]' '{$0=$7}1' infile

# 5  
Old 02-11-2010
Another one with awk:
Code:
awk -F"[ #]" '{print $7}' file

# 6  
Old 02-11-2010
Or:
Code:
awk -F'client |#' '$0=$2' infile


Last edited by radoulov; 02-11-2010 at 08:44 AM.. Reason: Corrected, sorry for the noise :)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with Expect script for pulling log files size larger than 500Mb;

I am new at developing EXPECT scripts. I'm trying to create a script that will automatically connect to a several UNIX (sun solaris and HPUX) database server via FTP and pull the sizes of the listener/alert log files from specified server directory on the remote machines. 1. I want the script... (7 Replies)
Discussion started by: mikebantor
7 Replies

2. Shell Programming and Scripting

Partial file pulling

I am connecting to another server through sftp. I am running one batch script to pull file from another server. sometimes i am receiving partial files. I am using below commands in batch script. ls -ltr new.txt mget new.txt bye The file is of 1 MB only.In most of the cases , i received... (6 Replies)
Discussion started by: srinath01
6 Replies

3. Shell Programming and Scripting

Pulling Data, Then Moving to the Next File

I'm scanning a list of emails- I need to pull 2 pieces of data, then move to the next file: Sender's Email Address Email Date I need these to be outputted into a single column- separated by a ",". Like this: Email1's Address, Email1's Date Stamp Email2's Address, Email2's Date Stamp... (4 Replies)
Discussion started by: sudo
4 Replies

4. UNIX for Dummies Questions & Answers

Pulling Parms from Config File

Hello all, I'm working on a general script for something at work. I'm an up-and-comer backup for a Shell Scripter this company has had for 35 years lol. Anyway, I have a config file I'm trying to pull Variables from as the Config File is used for multiple scripts. Does the below make sense and... (7 Replies)
Discussion started by: phunk
7 Replies

5. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

6. Shell Programming and Scripting

Problem in redirecting ftp msgs to a log file

Hi all.. The following set of statements is used in a shell script to ftp a file to a remote machine I want to redirect the ftp messages thrown by the first ftp statement to a log file. As you can see there is a logic downstream to decide if the ftp was a success or not. But i am not... (5 Replies)
Discussion started by: hareeshkumaru
5 Replies

7. Shell Programming and Scripting

Pulling data and following lines from file

I saw a few posts close to what i want to do, but they didn't look like they would work exactly.. or I need to think out of the box on this. I have a file that I keep server stats in for my own performance analysis. this file has the output from many commands in it (uptime, vmstats, ps, swap... (2 Replies)
Discussion started by: MizzGail
2 Replies

8. Shell Programming and Scripting

pulling a column from a file in ksh

I would like to pull a column from a file and place it in a variable: The file would look like this: N.Korea gibberish garbage S.Korea gibberish garbage USA gibberish garbage Iraq gibberish garbage Canada gibberish garbage and items in the first... (8 Replies)
Discussion started by: dangral
8 Replies

9. UNIX for Dummies Questions & Answers

pulling the following line from a file

I have return files from a process that has then original input record followed on the next line by a response record..either AA,........... for accepted or EE,.......... for errored. i.e 11,new,123 AA,accepted 12,exist,443 EE,rejected 13,old,223 AA,accepted I want to write a small... (4 Replies)
Discussion started by: peter.herlihy
4 Replies

10. UNIX for Advanced & Expert Users

Pulling out fields from a file

Hi, I have a file that contains 1400 lines similar to the one shown below: NAME=sara, TOWN=southampton, POSTCODE=SO18777, EMAIL=sara@hotmail.com, PASSWORD=asjdflkjds etc etc (note: this is one line). Each line has the same fields, but on each line they are in a different order. Eg. the line... (2 Replies)
Discussion started by: Saz
2 Replies
Login or Register to Ask a Question