LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to create a list of values (https://www.linuxquestions.org/questions/programming-9/script-to-create-a-list-of-values-575650/)

Jasper73 08-08-2007 10:37 AM

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

theNbomr 08-08-2007 10:48 AM

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.

druuna 08-08-2007 10:49 AM

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.

pwc101 08-08-2007 11:00 AM

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.

theNbomr 08-08-2007 11:40 AM

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.

Jasper73 08-09-2007 02:01 AM

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


All times are GMT -5. The time now is 05:41 AM.