How to grep in one file and execute for every match a command?
File:
foo
bar
42
foo
bar
I want to execute to execute for example date for every match on foo.
Following try doesn’t work:
grep file foo | date %s.%N
How to do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
More readably in a script:
For each line of input,
readwill put the value into the variable$line, and thewhilestatement will execute the loop body betweendoanddone. Since the value is now in a variable and not stdin, I’ve usedechoto push it back into stdin, but you could just dodate %s.%N "$line", assuming date works that way.Avoid using
for line in `grep file foo`which is similar, becauseforalways breaks on spaces and this becomes a nightmare for reading lists of files:would fail with
for.