LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-11-2010, 12:05 PM   #1
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Rep: Reputation: 16
Trying to read the contents of a file


Hello All -

I am trying to read the contents of a file into something else.

I have a file.txt that I am working with, I want to read the file and take the data and run some commands with the data that it read. So if it read www.yahoo.com I want to be able to nslookup.

Does that make sense? I have been trying to use the read command but that does not seem to work. I even was trying to read filename | > filename to see if I could even read any of the data at all. Nothing is working. Any ideas?

Tarken
 
Old 01-11-2010, 12:20 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 570

Rep: Reputation: 120Reputation: 120
Code:
for i in `cat file`
do
  nslookup  $i
done
 
Old 01-11-2010, 12:22 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
http://mywiki.wooledge.org/BashFAQ/001

Please provide details and/or code
 
Old 01-11-2010, 12:27 PM   #4
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by devnull10 View Post
Code:
for i in `cat file`
do
  nslookup  $i
done
That worked perfectly. Thanks! Now how does that bit of code work? I assume it is a for loop? What is the "i"?

Tarken
 
Old 01-11-2010, 12:31 PM   #5
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 570

Rep: Reputation: 120Reputation: 120
i is just a variable name, you could have used anything (well, any valid variable name of course) there instead (j, k, banana, var1, etc etc ...).
So, the `cat filename` bit executes the command cat filename and then the for loop loops over each line in the output.
Then we execute the nslookup command on each of those lines.

You could equally do it something like, but ultimately you need to use one loop or another to loop through the lines in the file. :

Code:
cat filename | while read i
do
  nslookup $i
done

Last edited by devnull10; 01-11-2010 at 12:32 PM.
 
Old 01-11-2010, 12:33 PM   #6
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Can I also do a slew of other commands as well as nslookup within the same script? Like pings and traceroutes?
 
Old 01-11-2010, 12:38 PM   #7
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 570

Rep: Reputation: 120Reputation: 120
Yes of course, just replace nslookup with the command you wish to execute.
 
Old 01-11-2010, 04:55 PM   #8
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Is there a way to have it not just read one file. But have it be able to do any file?
 
Old 01-11-2010, 05:22 PM   #9
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 570

Rep: Reputation: 120Reputation: 120
Yes, you just change filename to match one or more files.
If you aren't bothered which file the line has come from (ie, you just want the output from the command) and say all your files are called "file001.txt, file002.txt, file003.txt..." then the following would do this:

Code:
for i in `cat file*.txt`
do
  nslookup  $i
done
You can use regular expressions etc if you want. If it's a simple as querying every file in a directory then just do "cat *", assuming you are in that directory, if not then you will need to include the path - "cat /home/usera/demo/*".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to extract a .exe file to read its contents BobNutfield Slackware 13 07-30-2008 07:07 PM
How to read contents of CPU registers? irfanhab Programming 1 12-28-2004 08:04 AM
Can mount as user, but not read contents Rikra Linux - Hardware 13 12-07-2004 06:31 AM
How to read and change the contents of a text file? Bassam Linux - General 11 02-26-2004 04:08 AM
Read contents of file and execute command cosiek Programming 3 09-30-2003 11:07 AM

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

All times are GMT -5. The time now is 10:41 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