Extract substring after delimiter using GNU awk or awk

5,068

Please find the command

echo "/dev/sda: ST500LT012-9WS142: 47°C" | awk -F ":" '{print $3}'

output:

 echo "/dev/sda: ST500LT012-9WS142: 47°C" | awk -F ":" '{print $3}'
 47°C
Share:
5,068

Related videos on Youtube

Twissell
Author by

Twissell

Updated on September 18, 2022

Comments

  • Twissell
    Twissell over 1 year

    I would like to extract substring after colon delimiter for my practical needs using GNU awk utility. For example I can extract the temperature of hdd value using cut as follows

    hddtemp /dev/sda | cut -d ':' -f3
    

    The same example written in sed

    hddtemp /dev/sda | sed 's/.*\://'
    

    For glory of Saint Completeness and GNU Trinity (awk, cut, sed) I wonder, can GNU awk do such thing?

    Sample input: /dev/sda: ST500LT012-9WS142: 47°C

    Required output: 47°C

    • Praveen Kumar BS
      Praveen Kumar BS over 5 years
      Please post sample input and required output
    • Twissell
      Twissell over 5 years
      @PraveenKumarBS, done!
    • αғsнιη
      αғsнιη over 5 years
      ... |awk '{ print $NF }'
    • Twissell
      Twissell over 5 years
      @don_crissti, Yep! awk -F ":" '{print $NF}' also works well. But in that topic the answer wasn't accepted.
    • don_crissti
      don_crissti over 5 years
      (Sadly...) The best answer isn't always the accepted one...