Skip to content
Advertisement

Linux bash script removing part of string line by line

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement