2

I am trying to print a sum in the middle of a line. My script prompts for a user name and then pulls a total number of processes and a total of time for that user from a file.

I want to print a sentence with the totals in the middle of the sentence.

Using awk I can get the totals for processes and minutes but am unable to print the results of an equation in the middle of a sentence. My equation's result shows up after the sentence.

I feel like I need to use an anchor like in HTML. I am using PuTTY.

 oliver|Login|100
 smith|Login|20
 oliver|Time|25
 oliver|Time|25
 smith|Time|20
 Anne|Login|100

Example: User name? oliver
oliver has run 3 processes for a total of 150 minutes

 case $choice" in
   1) echo -n "User name?"
    read username
    echo "$username has run processes for a total of minutes"
    ;;

I would like to get a total for processes and minutes for the specific user name entered. Then insert those numbers into the sentence where they belong.

closed as unclear what you're asking by Sparhawk, jasonwryan, G-Man, don_crissti, vonbrand Nov 14 '15 at 10:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I don't think I understand what you're trying to do. Could you edit your post with: what you're using, what you've tried so far, example(s) of what you want the behaviour to be, and example(s) of what you're seeing now? – Michael Homer Nov 14 '15 at 6:04
  • @Jake Please verify that the edited question is correct. More importantly, please edit and show the actual code that was not doing what you want. Describing the incorrect output is not enough, you need to show both the code and the resulting output whether it's correct or not. Thank you and good luck. – RobertL Nov 15 '15 at 2:07
2

The following script takes its args from the command line rather than a prompt. This makes it easier to re-use from other scripts and allows the user to specify multiple usernames.

$ cat jake.sh 
#! /bin/sh

inputfile='input.txt'

for u in "$@" ; do
    awk -F'|' -v u="$u" '$1 == u { sum = sum + $3 ; count++} 
        END {print u " has run " count " processes for a total of " \
             sum " minutes."}' < "$inputfile"
done

Note how the shell script passes the shell variable $u to the awk script via the -v u="$u" option. That avoids having to use double-quotes around the awk script to use the shell $u directly...as that would require escaping every instance of $1, $3, and every " in the awk script with a backslash \.

The input is same as in your example:

$ cat input.txt 
oliver|Login|100
smith|Login|20
oliver|Time|25
oliver|Time|25
smith|Time|20
Anne|Login|100

And here's the output:

$ ./jake.sh oliver smith Anne
oliver has run 3 processes for a total of 150 minutes.
smith has run 2 processes for a total of 40 minutes.
Anne has run 1 processes for a total of 100 minutes.

If you really wanted it to prompt for the username, the script would look like this:

#! /bin/sh

inputfile='input.txt'

echo -n "User name?"
read username

awk -F'|' -v u="$username" '$1 == u { sum = sum + $3 ; count++} 
    END {print u " has run " count " processes for a total of " \
         sum " minutes."}' < "$inputfile"
  • I'll also mention for OP's benefit (as cas mentioned to me himself) that perl is a great language for when you need to combine shell features with awk features. In this case, awk works fine, as above. – Wildcard Nov 14 '15 at 6:31
  • That's exactly what I was looking of thank you. – jake Nov 14 '15 at 6:53

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