-1

After greping output of command i have get below result:

PAPU 0  
1000         GPRS ATTACH SUCC GB         2400       2382       2333       (10) 
1244         GPRS ATTACH FAIL GB         1216       1219       1252       (10) 
16000        GPRS ATTACH SUCC IU         986        986        1027       (10) 
16001        GPRS ATTACH FAIL IU         170        185        171        (10) 
2000         PDP-C ACTIVATION SUCC GB    5356       5138       5030       (10) 
2109         PDP-C ACTIVATION FAIL GB    13         15         54         (10) 
17000        PDP-C ACTIVATION SUCC IU    3637       3880       3887       (10) 
17001        PDP-C ACTIVATION FAIL IU    257        341        336        (10) 

Now i want to export some details to database. But i tried many variants and all unsuccess. As I know to get like below result i must use 'sed' or 'awk'. How i can get like below result?

PAPU_0  
2400
1216
986
170
5356
13
3637
257

Thanks.

Javid
  • 47
  • 2
  • 8
  • What is the original command that is giving this output? Also, what is the separator between columns? – fedorqui May 27 '15 at 10:23
  • The original command is not linux command. It is other vendor based command. Separator, i think is TAB or SPASE. I am not sure. – Javid May 27 '15 at 10:26
  • Not clear why `PAPU 0` becomes `PAPU_0`. Make sure what is the original input, since this space in between `PAPU` and `0` changes things. – fedorqui May 27 '15 at 10:27
  • PAPU 0 > PAPU_0 because in output 8 PAPU's and under each one 8 line like above. Also for sending the output to db , it will be more accesptable. If you have other idea , acceptable. – Javid May 27 '15 at 10:30
  • It is quite unclear what you mean here. Try to give a better explanation and the logic you want to apply. 3rd column? 6th field? – fedorqui May 27 '15 at 10:32
  • It does not matter. 3rd column or 6th field. I want to get for each PAPU\s[:digit:] like above output. I have PAPU 0, PAPU 1, PAPU 2 .... – Javid May 27 '15 at 10:35

2 Answers2

1

You can do it by following steps:

  1. first read all the lines in an array check
  2. iterate the each line you got in array

Code:

for i in "${check[@]}"
do
set  -- $i
# now you know that last 4th column contains the needed entries
# lets say you stored the length of array in var `length`
value=$(echo $(($length-3)))
echo $value
done
Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
0

You can use this awk program:

awk '{ if ($6 == "") { sub(/ /, "_", $0); print $0; } else { print $6; } }'

If the sixth column is not present, it changes spaces to underscores (changing PAPU 0 to PAPU_0), and otherwise it just prints the sixth column.

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • Note `print $0` is not necessary, `print` suffices. You can also say `awk '{if (NF>=6) {print $6} else {sub(/ /, "_", $0); print}}' file`. – fedorqui May 27 '15 at 10:41
  • @fedorqui You don't need the `$0` in sub, it is default. – 123 May 27 '15 at 11:00
  • @User112638726 OK, then it is even shorter and cleaner: `awk '{if (NF>=6) {print $6} else {sub(/ /, "_"); print}}' file` By the way: are you JID? – fedorqui May 27 '15 at 11:01
  • @fedorqui No... . Also if we wanted to go super short `awk '{if(NF<6)sub(/ /,"_");else $0=$6}1'` ;) – 123 May 27 '15 at 11:59