0

I'm trying to echo the information/variables for each line of an alarmfile (named critical), using while read line to get each line of the alarmfile (might be a lot of lines on critical file):

while read line
do
{
s=`grep -Po '(?<=(sender:)).*' critical| cut -d ',' -f1` 
t=`grep -Po '(?<=(time:)).*' critical| cut -d '.' -f1` 
d=`grep -Po '(?<=(.1.3.6.1.4.1.2285.1.1.8:)).*' critical| cut -d ',' -f1` 
echo "Sender: $s" >>critical_body
echo "Time: $t" >>critical_body
echo "Description: $d" >>critical_body
}
done < critical

The output i need is to get it structured like below:

Sender:  xyz1
Time:  2021/08/11 22:14:19
Description: Lost communication with device

Sender: xyz2
Time: 2021/08/13 15:58:02
Description: Memory usage is above normal: 10% free

But i get the following, it seems that the variables values are printed all in one iteration separated with a /n and not one by one as i want.

Sender:  xyz1
 xyz2
Time:  2021/08/11 22:14:19
 2021/08/13 15:58:02
Description: Lost communication with device
Memory usage is above normal: 10% free
Sender:  xyz1
 xyz2
Time:  2021/08/11 22:14:19
 2021/08/13 15:58:02
Description: Lost communication with device
Memory usage is above normal: 10% free

Can someone help me? I am trying to find what's wrong, im relatively new on bash scripting.

cat critical
2021-08-11 22:14:19,550 INFO  [com.optimization.ems.snmp.FnTrapReceiver] raw trap record inserted: com.optimization.ems.ems.model.snmp.RawTrapid: null, oid: .1.3.6.1.4.1.2285.1.2.14, time: 2021/08/11 22:14:19.547, sender: xyz1, version: 2, varbind: .1.3.6.1.2.1.1.3.0:5d 11h 0m 35s 350ms,.1.3.6.1.6.3.1.1.4.1.0:.1.3.6.1.4.1.2285.1.2.14,.1.3.6.1.4.1.2285.1.1.6:xyz1,.1.3.6.1.4.1.2285.1.1.8:Lost communication with device,.1.3.6.1.4.1.2285.1.1.3:5,
2021-08-13 15:58:02,083 INFO  [com.optimization.ems.snmp.FnTrapReceiver] raw trap record inserted: com.optimization.ems.ems.model.snmp.RawTrapid: null, oid: .1.3.6.1.4.1.2285.1.2.3, time: 2021/08/13 15:58:02.79, sender: xyz2, version: 2, varbind: .1.3.6.1.2.1.1.3.0:98d 21h 39m 6s 60ms,.1.3.6.1.6.3.1.1.4.1.0:.1.3.6.1.4.1.2285.1.2.3,.1.3.6.1.4.1.2285.1.1.2:15:58:02 13/08/2021,.1.3.6.1.4.1.2285.1.1.3:5,.1.3.6.1.4.1.2285.1.1.8:Memory usage is above normal: 10% free,
5
  • I suggest you use sed to extract the tokens. Commented Sep 6, 2021 at 10:26
  • Please post an example of 'critical file' Commented Sep 6, 2021 at 10:31
  • i posted the critical file example. Commented Sep 6, 2021 at 10:34
  • you are processing the entirety of critical for every line and ignoring $line variable Commented Sep 6, 2021 at 10:39
  • @jhnc how can i make use of the $line variable inside of the loop? Commented Sep 6, 2021 at 11:23

3 Answers 3

1

Something like this:

while read line; do
    time=${line/*time:/}
    time=${time/.*, sender:*/}
    sender=${line/*sender: /}
    sender=${sender/, version:*/}
    description=${line/*.1.1.8:/}
    description=${description/,*/}
    printf 'Sender: %s\nTime: %s\nDescription: %s\n\n' "$sender" "$time" "$description"
done < critical

Output will be:

Sender: xyz1
Time:  2021/08/11 22:14:19
Description: Lost communication with device

Sender: xyz2
Time:  2021/08/13 15:58:02
Description: Memory u sage is above normal: 10% free
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this script. It is a bit of a hack and there may be a more efficient way to achieve this.

 for c in critical;           
    do      
    out="critical.outfile"; 
    outNew="crit.out "    ;     
    sed -E 's/.*(sender[^,]*).*/\1/1' "$c" > "$out";           
    sed -E 's/.*(time[^,]*).*/\1/' "$c" >> "$out";          
    sed -E 's/.*[0-9]:([^0-9].*[a-z]),.*/Description: \1/' "$c" >> "$out";
    awk '!r[$1]++' "$out" > $outNew;      
    echo >> $outNew;     
    awk 'r[$1]++' "$out" >> $outNew;
    cat $outNew  
done

Output

sender: xyz1
time: 2021/08/11 22:14:19.547
Description: Lost communication with device

sender: xyz2
time: 2021/08/13 15:58:02.79
Description: Memory usage is above normal: 10% free

4 Comments

:) better but its giving me: sender: xyz1 sender: xyz2 time: 2021/08/11 22:14:19.547 time: 2021/08/13 15:58:02.79 Description: Lost communication with device Description: Memory usage is above normal: 10% free
Works perfectly for me. What you have there is the output of the $out variable file. I will edit my answer so there is a file with the output
it works only when there are 2 lines, but for more lines its giving again the same sender: xyz sender: xyz sender: xyz time: 2021/08/13 15:58:02.79 time: 2021/08/13 15:14:19.572 time: 2021/08/13 15:34:19.549 Description: Memory usage is above normal: 10% free Description: Lost communication with device Description: Lost communication with device :(
As mentioned, it is a bit of a hack according to the example provided. It will fail at 2+ lines due to the awk
0

For each line you use grep with, you can pipe to the echo command to output all in one go. Afterwards you can grep the time, and then the description. After the description you can use echo on its own to have a new line, before you start a new iteration of the loop.

For debug and troubleshoot more easily test the grep for sender sender, and see if the output is correct on the command line, then you can add this into the script, when the output for the sender is correct, the other two should be easier to amend after.

s=`grep -Po '(?<=(sender:)).*' critical| cut -d ',' -f1` | echo "Sender: $s" >>critical_body

1 Comment

Hello. I tried this, but it's not printing the value of the variable in one go, it only prints Sender: . im checking

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.