LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2007, 10:37 AM   #1
Jasper73
LQ Newbie
 
Registered: May 2004
Posts: 3

Rep: Reputation: 0
Script to create a list of values


I wish to create a script that will look in a file for a certain value (in this case an IP address) and then output it to another file on a seperate line. I have seen it is possible to use AWK and then print the value where it is on the line, the trouble is the value can move from line to line. How can I go about resolving this please?
Newbie to programming so any explanations on what the command is doing is greatly appreciated.
Sorry if this has been covered but wasnt sure on what to search for!
TIA
Jasper
 
Old 08-08-2007, 10:48 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
A nice one-liner question.
Shouldn't take long for someone to trump this.
Code:
perl -e 'while(<>){if($_ =~ m/YourPatternHere/){print $&,"\n";}}' filename.ext > outputfile.log
--- rod.
 
Old 08-08-2007, 10:49 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

A lot of unix/linux tools are line driven (by default, you can change this behaviour), they parse one line at the time and the tools can do something with/to that line. AWK is also line driven.

Something line this: awk '/10.0.0.1/ { print $0 }' infile will print all lines (print $0) if 10.0.0.1 is found (/10.0.0.1/) anywhere on a line, all other lines are not printed.

If you want to put the output into another file you can redirect the output: awk '/10.0.0.1/ { print $0}' infile > outfile. The > outfile part makes sure that the output is put in a file called output.

Here's a good link to the (gnu) awk documentation: The GNU Awk User's Guide

Hope this helps.
 
Old 08-08-2007, 11:00 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
grep your_search_string input_file.txt > output_file.txt
That will print all the text on the line containing "your_search_string" - this is similar to the output of druuna's awk statement.
 
Old 08-08-2007, 11:40 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This has to be a simple as it can get...
Code:
grep -oh 'YourPatternHere' input_file(s) > output_file.txt
...unless you need to do...
Code:
grep -ohe 'YourRegularExpressionHere' input_file(s) > output_file.txt
--- rod.
 
Old 08-09-2007, 02:01 AM   #6
Jasper73
LQ Newbie
 
Registered: May 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Many thanks to all who have replied, there seems to be many different ways I can get the output I want....I will need to mess about with a file until I get it right.

Once many thanks to all who have replied...most helpful!!!!!

Cheers
Jasper
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting function return values in a Bourne script? ocicat Programming 5 08-01-2007 07:01 AM
how to create a simple script for installing a list of files zbenjudah Debian 28 04-16-2007 03:03 PM
gtk.ListStore - python - retrieving the list values? vharishankar Programming 3 11-30-2006 09:15 PM
Script to input values into firefox. WhiskeyTangoFoxtrot Programming 5 11-01-2006 12:38 PM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration