Register
It is currently Sat Jul 26, 2014 1:14 pm

Text output piped through bash script problem


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Nov 21, 2011 3:33 am   

Joined: Fri May 07, 2010 4:04 am
Posts: 7
Hello,

I have a script that outputs lines like this one:
Code:
-VOIP- 11/20/2011 02:04:09 AM EET  ONTL01       OPMD  028* BLCSIC  <> mgw       *    DG100 (DG075) OUT        ONL         000076


Now, I am trying to write another one that gets those lines from a pipe and keep the date, time, the number after the word "OPMD" (OPM, 028), the words OUT & ONL (CURRENT & PREVIOUS) and the last number (INSTANCE, 000076).

This is what I do:

Code:
#!/bin/bash
# translate_issw_alarms.sh

while read DATA; do
        DATE=`echo $DATA | grep "mgw " | awk '{print $2}'`
        echo "Date: "$DATE

        TIME=`echo $DATA | grep "mgw " | awk '{print $3" "$4}'`
        echo "Time: "$TIME

        OPM=`echo $DATA | grep "mgw " | awk '{print $8}' | awk -F "*" '{print $1}'`
        echo "OPM: "$OPM

        CURRENT=`echo $DATA | grep "mgw " | awk '{print $15}'`
        echo "Current: "$CURRENT

        PREVIOUS=`echo $DATA | grep "mgw " | awk '{print $16}'`
        echo "Previous: "$PREVIOUS

        INSTANCE=`echo $DATA | grep "mgw " | awk '{print $17}'`
        echo "Instance: "$INSTANCE
        echo
done


and for example for those 3 lines:

Code:
VOIP- 11/20/2011 12:05:03 AM EET  ONTL01       OPMD  003* BLCSIC  <> mgw       *    DG100 (DG000) OUT        ONL         000279           
-VOIP- 11/20/2011 12:05:13 AM EET  ONTL01       OPMD  003* BLCSIC  <> mgw       *    DG000 (DG100) ONL        OUT         000279           
VOIP- 11/20/2011 12:07:10 AM EET  ONTL01       OPMD  018* BLCSIC  <> mgw       *    DG075 (DG000) ONL        ONL         000383


I get this output:

Quote:
Date: 11/20/2011
Time: 12:05:03 AM
OPM: 003
Current: test_echo.sh
Previous: translate_issw_alarms.sh
Instance: DG100


Date: 11/20/2011
Time: 12:05:13 AM
OPM: 003
Current: test_echo.sh
Previous: translate_issw_alarms.sh
Instance: DG000


Date: 11/20/2011
Time: 12:07:10 AM
OPM: 018
Current: test_echo.sh
Previous: translate_issw_alarms.sh
Instance: DG075


The "*" mess up my output somehow but I cannot understand why!....

This is what I have in my directory:

Code:
-rwxr-xr-x 1 portal portal    192 2011-11-21 09:44 issw_alarms_translated.sh
-rwxr-xr-x 1 portal portal    242 2011-11-18 15:03 issw_logging_translated.sh
-rw-r--r-- 1 portal portal 505594 2011-11-21 10:00 logs
-rwxr-xr-x 1 portal portal    562 2011-11-18 15:00 test_echo.sh
-rwxr-xr-x 1 portal portal    623 2011-11-21 11:15 translate_issw_alarms.sh



I have tried to replace "*" with nothing ("") using sed but the second "*" (eg. mgw * DG075) does go away and I have no idea why....

Could you help me please??


Top
 Profile  
 PostPosted: Mon Nov 21, 2011 6:29 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Hi,

$DATA should be quoted, for, amongst other thing, * to be not subject of path expansion.

you know, awk can grep stuffs
Code:
echo "$DATA" | awk '/mgw/{ print $12}'

but this also could be done using only bash.
Code:
echo "foo bar baz" |while read -r no field rest; do echo "$field"; done


Top
 Profile  
 PostPosted: Mon Nov 21, 2011 11:17 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
awk(1) is the tool for the job.

Code:
#!/usr/bin/awk -f
# translate_issw_alarms.awk

$11 == "mgw" \
{
    sub("*", _, $8)

    print "Date:", $2, "\nTime:", $3, $4
    print "OPM:", $8
    print "Current:", $15, "\nPrevious:", $16
    print "Instance:", $17, "\n"
}


You run it like this:
Code:
issw_alarms_translated.sh | ./translate_issw_alarms.awk

or

./translate_issw_alarms.awk < <(./issw_alarms_translated.sh)


Though, since you did your script in bash(1), I'll give you a proper solution in this language:

Code:
#!/bin/bash
# translate_issw_alarms

while read -r _ date t1 t2 _{,,} opm _ \
              _ x _{,,} curr prev inst; do
   
    if [[ $x != mgw ]]; then
        continue
    fi 

    printf 'Date: %s\nTime: %s %s\n' "$date" "$t1" "$t2"
    printf 'OPM: %s\n' "${opm%\*}"
    printf 'Current: %s\nPrevious: %s\n' "$curr" "$prev"
    printf 'Instance: %s\n\n' "$inst"
done \
    < <(./issw_alarms_translated.sh)


Top
 Profile  
 PostPosted: Tue Nov 22, 2011 1:29 am   

Joined: Fri May 07, 2010 4:04 am
Posts: 7
thank you all for for help, yes it was the quotes...
though your solutions are a bit complicated for me, I am new in bash scripting but I am getting better day-by-day! :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP