Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. The output of the command is written to another file. How do I go about doing that? I don't know where to start.

share|improve this question

4 Answers 4

up vote 7 down vote accepted

Use while read loop:

: > another_file  ## Truncate file.

while IFS= read -r LINE; do
    command --option "$LINE" >> another_file
done < file

Another is to redirect output by block:

while IFS= read -r LINE; do
    command --option "$LINE"
done < file > another_file

Last is to open the file:

exec 4> another_file

while IFS= read -r LINE; do
    command --option "$LINE" >&4
    echo xyz  ## Another optional command that sends output to stdout.
done < file

If one of the commands reads input, it would be a good idea to use another fd for input so the commands won't eat it:

while IFS= read -ru 3 LINE; do
    ...
done 3< file

Finally to accept arguments, you can do:

#!/bin/bash

FILE=$1
ANOTHER_FILE=$2

exec 4> "$ANOTHER_FILE"

while IFS= read -ru 3 LINE; do
    command --option "$LINE" >&4
done 3< "$FILE"

Which one could run as:

bash script.sh file another_file

Extra idea. Use readarray:

readarray -t LINES < "$FILE"
for LINE in "${LINES[@]}"; do
    ...
done
share|improve this answer
    
Thank You! this is the most complete answer I ever recieved. I used the idea that allows accepting arguments! This was veeery helpful! –  WojciechF Aug 11 at 17:35
    
@WojciechF Welcome. If you find the answer most appropriate to you question please accept it by clicking the check mark thanks :) –  konsolebox Aug 11 at 17:37

Another option is xargs.

e.g.

cat file | xargs -I{} -d"\n" command --option {} other args

{} is the place holder for the line of text.


I am using gnu xargs, on gnu+linux. Other xargs may not have all of the same features.

share|improve this answer
    
reducible to <file xargs -L 1 -I{} command --option {} other args –  1_CR Aug 12 at 12:13
    sed "s/'/'\\\\''/g;s/.*/\$* '&'/" <<\FILE |\
    sh -s -- command echo --option
all of the{&}se li$n\es 'are safely shell
quoted and handed to command as its last argument
following --option, and, here, before that echo
FILE

OUTPUT

--option all of the{&}se li$n\es 'are safely shell
--option quoted and handed to command as its last argument
--option following --option, and, here, before that echo
share|improve this answer
ed file.txt
%g/^/s// /
2,$g/^/-,.j
1s/^/command/
wq
chmod 755 file.txt
./file.txt

Take all the lines of a file and pass them as arguments to a single command i.e.,

command line1 line2 line3 ....

If you need the --option flag to precede each line change the second command to:

%g/^/s// --option /
share|improve this answer
    
What -1 for using ed...really? –  user2217522 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.