0

I have a bash script that is parsing files containing information about processes running on a server. Everything works except the output.

Target output
tomcat7 Running Monitored 3025 18d 2h 16m 3.6% 0.0%

What it actually is outputing
0.0%2h 16m ing

Script portion doing the parsing and output

for SERVER in $SERVERS ; do
    SYSTEM=$(sed -n '/System/{p; n;p; n;p; n;p; n;p; n;p}' $H_DIR/$SERVER.txt)
    sed -n '/Process/{p; n;p; n;p; n;p; n; n;p; n;n;n; n;p; n; n;p}' $H_DIR/$SERVER.txt > $H_DIR/procs.txt
    split --lines=7 $H_DIR/procs.txt $H_DIR/procs.txt.
    for PROC in $H_DIR/procs.txt.?? ; do
        PROCESS=$(cat $PROC | head -1 | tail -1 | cut -d "'" -f2)
        STATUS=$(cat $PROC | head -2 | tail -1 | awk '{ print $NF }')
        MONITOR=$(cat $PROC | head -3 | tail -1 | awk '{ print $NF }')
        PID=$(cat $PROC | head -4 | tail -1 | awk '{ print $NF }')
        UPTIME=$(cat $PROC | head -5 | tail -1 | awk '{ print substr($0, index($0, $2)) }')
        PCPU=$(cat $PROC | head -6 | tail -1 | awk '{ print $NF }')
        PMEM=$(cat $PROC | head -7 | tail -1 | awk '{ print $NF }')
        echo $PROCESS $STATUS $MONITOR $PID $UPTIME $PCPU $PMEM
    done
    rm -f $H_DIR/procs.*
    rm -f $H_DIR/$SERVER.txt
done

raw file being parse

Process 'tomcat7'
  status                            Running
  monitoring status                 Monitored
  pid                               3025
  uptime                            18d 2h 30m
  memory percent                    3.6%
  cpu percent                       0.0%
3
  • Your sample file and output indicate that you're only printing the last element from each line. Why kill yourself? take out all of that stuff and experment with echo "$procInfo" | awk '{printf $NF}:END{print ""}' .. How you get proc info should also be simple. Good luck. Commented May 22, 2014 at 21:54
  • @shellter : where did $procInfo come from? Commented May 22, 2014 at 22:14
  • 1
    Not a complete solution. How you get proc info should also be simple. hence a comment. Good luck to all. Commented May 22, 2014 at 22:19

1 Answer 1

2

On a hunch - your input files have the DOS carriage return line feed combination.

I added that to your file and got the same results you did.

See this question for how to remove the carriage return:

Remove carriage return in Unix

Using the suggested tr -d '\r' method for removing carriage return (and might as well remove single quotes at the same time) you could do something like this:

echo $(tr -d "\r\'" < $PROC | awk 'NR==5{print substr($0,index($0,$2))}{print $NF}')

or if you need each variable assigned then something like this

VARS=$(tr -d "\r\'" < $PROC | awk 'NR==5{print substr($0,index($0,$2))}{print $NF}')
read PROCESS STATUS MONITOR PID UPTIME PCPU PMEM <<<$VARS
echo $PROCESS $STATUS $MONITOR $PID $UPTIME $PCPU $PMEM

Either way, output is

tomcat7 Running Monitored 3025 18d 2h 30m 30m 3.6% 0.0%
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I did some testing and the program that outputs the raw information for the process is putting the carriage returns in.
After each field is printed the carriage return brings the cursor back to the beginning of the line and you overwrite the output with the next field... the longest earlier fields show up, the only field guaranteed to be unmodified is the last one 0.0%

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.