Take input from a file and perform a command

Reply

Join Date: Nov 2005
Posts: 2
Reputation: script_noob is an unknown quantity at this point 
Solved Threads: 0
script_noob script_noob is offline Offline
Newbie Poster

Take input from a file and perform a command

 
0
  #1
Nov 17th, 2005
input.txt is a file that contains filenames that have been piped into it from an external program.

for example...

input.txt has the following in it
filename1.txt
filename2.txt
filename3.txt

I would like to perform an 'rm' command on the filenames contained in input.txt

So instead of potential hours of work deleting each individual filename contained in input.txt, I would like to automate this task by a simple bash script or using another program like awk.

I have toyed with using the 'exec' command in bash and contemplated that there must be an easier way using awk or another such program.

anybody got a quick one or two liner that would demonstrate to me how I would accomplish this?
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Take input from a file and perform a command

 
0
  #2
Nov 17th, 2005
Shell Scripting Syntax (Toggle Plain Text)
  1. for i in `cat input.txt`;
  2. do
  3. rm -f $i
  4. done

...Something like that should do it. Basically, it should count each line as an item, and the $i variable is going to be the name of the file on that line. I added the -f command because I don't want it to ask me "are you sure?" every time, like some distros make bash do. If you're not as brave, then you can omit the -f on the rm command.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 2
Reputation: script_noob is an unknown quantity at this point 
Solved Threads: 0
script_noob script_noob is offline Offline
Newbie Poster

Re: Take input from a file and perform a command

 
0
  #3
Nov 17th, 2005
That's just what I was looking for! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: rusman is an unknown quantity at this point 
Solved Threads: 0
rusman rusman is offline Offline
Newbie Poster

Re: Take input from a file and perform a command

 
0
  #4
Feb 4th, 2006
Originally Posted by alc6379
Shell Scripting Syntax (Toggle Plain Text)
  1. for i in `cat input.txt`;
  2. do
  3. rm -f $i
  4. done

...Something like that should do it. Basically, it should count each line as an item, and the $i variable is going to be the name of the file on that line. I added the -f command because I don't want it to ask me "are you sure?" every time, like some distros make bash do. If you're not as brave, then you can omit the -f on the rm command.
I am trying to write a script that accomplishes this same thing, but I am running into a problem.

Shell Scripting Syntax (Toggle Plain Text)
  1. for i in <filename>
  2. do
  3. <application command> <-option> $i
  4. done

What I'm finding is that the command is coming out something like this:
<application command> <-option> <filename>

Why is the name of the file being used as the variable instead of the text in the file?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 9
Reputation: Subterraneus is an unknown quantity at this point 
Solved Threads: 0
Subterraneus Subterraneus is offline Offline
Newbie Poster

Re: Take input from a file and perform a command

 
0
  #5
Feb 5th, 2006
you need to cat the file.
Oh, just press any key... no, no, no, not that one!

*sigh* Make it idiot-proof, and someone will make a better idiot...
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: craju45 is an unknown quantity at this point 
Solved Threads: 0
craju45 craju45 is offline Offline
Newbie Poster

Re: Take input from a file and perform a command

 
0
  #6
Mar 24th, 2006
Originally Posted by script_noob
input.txt is a file that contains filenames that have been piped into it from an external program.

for example...

input.txt has the following in it
filename1.txt
filename2.txt
filename3.txt

I would like to perform an 'rm' command on the filenames contained in input.txt

So instead of potential hours of work deleting each individual filename contained in input.txt, I would like to automate this task by a simple bash script or using another program like awk.

I have toyed with using the 'exec' command in bash and contemplated that there must be an easier way using awk or another such program.

anybody got a quick one or two liner that would demonstrate to me how I would accomplish this?

you can do in two ways.
1. write a shell script so that u need to just run the script

cat rm.sh

#!\bin\sh
for i in `cat input.txt`
do
rm $i
done

then u cnage the permisssion.
command prompt>chmod 777 rm.sh
just run the shell script
command prompt>rm.sh
this will delete all the files given in the list.txt
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,385
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: Take input from a file and perform a command

 
0
  #7
Mar 27th, 2006
If the only thing in the file is filenames simply do
rm `cat input.txt`
if there is more than just filenames, but say the third word of
every line is a filename then do the following:
rm `awk '{print $3}' input.txt`
(the fourth word would be $4, etc)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC