0

I have a text file, say, input.txt and I want to run a command and write the output to another text file, say, output.txt. I need to read values from input.txt, each value is in a line, then I need to insert them in the command then write the result in output.txt file. I tried the following and it works fine with me:

for i in `cat input.txt`; do command -m $i -b 100; echo $i; >> output.txt; done

Now, I need to make some improvements over this but I have little experience in Linux so I need some help.

What I need to do is: 1) Before each command result, I want to insert the value of i separated by comma. For example:

i1,result1 
i2,result2
i3,result3

2) I need to change the second fixed value that I used in my command from a fixed value (100) to a value read from input.txt. So, the new input file which contains two values, say, newinput.txt is as the following:

i1,value1
i2,value2
i3,value3

1 Answer 1

1

Try this, in bash:

IFS=','
while read i val; do
  echo -n "$i,"
  command $i $val
done < input.txt > output.txt
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I get: bash: syntax error near unexpected token 'do'
When I removed the ; before do, I got: while: command not found
I solved the issue. I was writing the script in the terminal. When I wrote it to a file and then execute it, it worked.
@user1810868 Typing the script to the terminal is not the issue. Rather, the issue is (probably) that the shell you are using when entering text is a csh variant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.