943,729 Members | Top Members by Rank

Ad:
Nov 17th, 2005
0

Take input from a file and perform a command

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
script_noob is offline Offline
2 posts
since Nov 2005
Nov 17th, 2005
0

Re: Take input from a file and perform a command

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Nov 17th, 2005
0

Re: Take input from a file and perform a command

That's just what I was looking for! Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
script_noob is offline Offline
2 posts
since Nov 2005
Feb 4th, 2006
0

Re: Take input from a file and perform a command

Quote 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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rusman is offline Offline
17 posts
since Feb 2006
Feb 5th, 2006
0

Re: Take input from a file and perform a command

you need to cat the file.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Subterraneus is offline Offline
9 posts
since Jan 2006
Mar 24th, 2006
0

Re: Take input from a file and perform a command

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
craju45 is offline Offline
3 posts
since Mar 2006
Mar 27th, 2006
0

Re: Take input from a file and perform a command

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)
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: re
Next Thread in Shell Scripting Forum Timeline: extracting db2 table records to csv





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC