LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2015, 06:03 AM   #1
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Rep: Reputation: Disabled
Read file and parse each row


Hi,

I have a file that contains rows like these:
Code:
128.199.248.105:54
217.12.210.54:54
77.66.84.233:443
77.66.84.233:5353
176.56.237.171:443
I need to read that file and for each row run this command:

Code:
dig google.com @ADDRESS -p PORT
in this case:

Code:
dig google.com @128.199.248.105 -p 54
dig google.com @217.12.210.54 -p 54
dig google.com @77.66.84.233 -p 443
dig google.com @77.66.84.233 -p 5353
dig google.com @176.56.237.171 -p 443
How can I do that?

Thanks
 
Old 08-23-2015, 06:10 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,598

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
Using what tools?

First, is this some kind of education assignment?
Second, what have you tried or considered so far?
Third, what are your tools? Is your use constrained to a certain shell, interpreter, or environment?
 
Old 08-23-2015, 06:15 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Since you have a file all you need to do is:
(1) Insert dig google.com @ at the beginning of each line,
(2) Replace all occurrences of the colon ( character with a space followed by a -p followed by a space

You now have a script file.

Make it executable and execute the script.

OK
 
Old 08-23-2015, 06:23 AM   #4
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
It's a script I'm writing for myself.
I'm using bash.
I tried to pul all addresses (substring that end with ":" character) in an array, but I don't how to extract the PORT and use it in the command.
 
Old 08-23-2015, 06:27 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by AnanthaP View Post
Since you have a file all you need to do is:
(1) Insert dig google.com @ at the beginning of each line,
(2) Replace all occurrences of the colon ( character with a space followed by a -p followed by a space

You now have a script file.

Make it executable and execute the script.

OK
^This will pretty much do it. But the ports can be an issue. How would you decide which port to use for which line (ip address) in the script?

Best,
HMW

Edit: Forget this part: How would you decide which port to use for which line (ip address) in the script? I didn't read the first post properly!!!

Last edited by HMW; 08-24-2015 at 01:52 AM.
 
Old 08-23-2015, 06:39 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Look up a while / read loop and what setting IFS might do for you. With just a little searching I am sure you could this trivially.
 
1 members found this post helpful.
Old 08-23-2015, 06:49 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by Hoxygen232 View Post
I tried to pul all addresses (substring that end with ":" character) in an array, but I don't how to extract the PORT and use it in the command.
Better perhaps to use sed - easy enough to separate the bits you want to keep and use, and replace the bits you don't.
 
Old 08-23-2015, 09:04 AM   #8
Hoxygen232
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
I'll try thanks
 
Old 08-24-2015, 05:38 AM   #9
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,598

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
Assuming your source file name is source.txt
and you want the results in my.script,
you might try something like this:
Code:
for foo in `cat source.txt` ; do
   IP=`echo ${foo}|cut -d: -f1`
   PORT=`echo ${foo}|cut -d: -f2`
   echo "dig google @${IP} -p ${PORT}"
done > my.script
Naturally, there are almost unlimited ways to do this depending upon what tools you want to use.
Bash string manipulation could easily pull apart the strings into IP and PORT without calling cut, but cut is clear and easy to understand, and performance is not really at issue with so small a file. The sed and awk utilities COULD be used, and perl would make it easy, but bash has everything you really need.
 
Old 08-24-2015, 05:40 AM   #10
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by grail View Post
Look up a while / read loop and what setting IFS might do for you. With just a little searching I am sure you could this trivially.
I had to try this approach, and I agree that using a while loop and setting IFS appropriately is probably the smoothest way to go. Three lines of code and you're done!

My output (with echo instead of an actual "dig" at Google!):
Code:
dig google.com @128.199.248.105 -p 54
dig google.com @217.12.210.54 -p 54
dig google.com @77.66.84.233 -p 443
dig google.com @77.66.84.233 -p 5353
dig google.com @176.56.237.171 -p 443
Best regards,
HMW
 
  


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
Perl read file and parse blocks Goni Programming 34 08-22-2010 09:01 AM
Help w/ script to read file and parse log message shyork2001 Linux - General 4 04-06-2010 11:48 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM
Help w/ script to read file and parse variables cslink23 Linux - General 18 11-26-2006 02:22 AM
C++ read csv file row into vector taban1 Programming 3 11-08-2004 02:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:38 PM.

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