1

I am pasting multiple commands into my command line and would like each line to execute and output the results sequentially.

I am pasting my input:

cat type_of_record.txt | grep 'type_of_record_new creating' | wc -l           
cat type_of_record.txt | grep 'type_of_record_modification found 1' | wc -l   
cat type_of_record.txt | grep 'type_of_record_modification found 0' | wc -l   
cat type_of_record.txt | grep 'type_of_record_inactivation found 1' | wc -l   

and getting output:

  469005
    9999
    5099
      25

But instead, would like each command to execute after each line break and have my output look like:

cat type_of_record.txt | grep 'type_of_record_modification found 1' | wc -l
469005
cat type_of_record.txt | grep 'type_of_record_modification found 0' | wc -l
9999
cat type_of_record.txt | grep 'type_of_record_inactivation found 1' | wc -l
5099                                                                       

Not sure if this is possible but it would save me a lot of time if I don't have to map each result back to the line where it came from

1

Just paste your clipboard into a heredoc:

$ sh -v << EOF
> cat type_of_record.txt | grep 'type_of_record_new creating' | wc -l           
> cat type_of_record.txt | grep 'type_of_record_modification found 1' | wc -l   
> cat type_of_record.txt | grep 'type_of_record_modification found 0' | wc -l   
> cat type_of_record.txt | grep 'type_of_record_inactivation found 1' | wc -l
> EOF
cat type_of_record.txt | grep 'type_of_record_new creating' | wc -l           
cat: type_of_record.txt: No such file or directory
       0
cat type_of_record.txt | grep 'type_of_record_modification found 1' | wc -l   
cat: type_of_record.txt: No such file or directory
       0
cat type_of_record.txt | grep 'type_of_record_modification found 0' | wc -l   
cat: type_of_record.txt: No such file or directory
       0
cat type_of_record.txt | grep 'type_of_record_inactivation found 1' | wc -l
cat: type_of_record.txt: No such file or directory
       0

In the above, I typed 'sh -v << EOF', then pasted the code from your question into the terminal, and then hit return and typed 'EOF'. If you do this sort of thing, make sure you carefully review the text you're pasting. You will probably want to quote the delimiter (eg sh << 'EOF') to avoid interpolation of any of the pasted text, but that's not necessary in this case.

But note that in this particular case, it seems better to use awk to count the matching records so that you only need to make one pass through the file.

3

That's not too simple to do if you're copy-pasting the commands to the terminal emulator. The problem here is that the terminal (that handles text input and display) is a distinct process from the shell (that prints your prompt and process the command line), and the terminal can't really know the meaning of the text you're pasting. So it can't wait for the previous command to finish, and for the shell to prompt again.

What you could do, would be to use set -v or set -x to have the shell print the commands as it runs them, or in that particular case, write a short shell loop to run the greps:

for pattern in 'type_of_record_new creating' \
    'type_of_record_modification found 1' \
    'type_of_record_modification found 0' \
    'type_of_record_inactivation found 1'
do
    printf "%s:\n%10d" "$pattern"
    grep -c -e "$pattern" type_of_record.txt
done

That would give output like this

type_of_record_new creating: 469005
type_of_record_modification found 1: 9999

though the exact format could of course be modified. If you want the numbers on separate lines, and aligned to the right, you could use command substitution and printf %d for the number of lines:

for pattern in ...
do
    printf "%s:\n%8d\n" "$pattern" "$(grep -c -e "$pattern" type_of_record.txt)" 
done
0

Using echo might do it

First, you would literally echo the command as a string, using single-quotes, then

Second, you would echo the command using double-quotes, which will execute the command

echo '$(date)'; echo "$(date)"

which gives

robert@pip2:/tmp$ echo '$(date)'; echo "$(date)"
$(date)
Fri May 18 07:30:27 PDT 2018

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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