You are not logged in.

#1 2011-01-02 15:27:32

STANKAR
Member
From: Czech Republic
Registered: 2010-11-16
Posts: 47

help with cat

Hi, i need help with formating output from text.

For example i have this output:

/dev/sda: WDC WD1600BEVT-22ZCT0: 37°C

And i need format this output to "37°C" only or "/dev/sda" only or "WDC WD1600BEVT-22ZCT0" only.

I know only how i can format output to "/dev/sda" :

cat text.txt | head -c 8

Thanks

Last edited by STANKAR (2011-01-02 17:16:53)

Offline

#2 2011-01-02 15:35:07

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: help with cat

echo "/dev/sda: WDC WD1600BEVT-22ZCT0: 37°C" | cut -c34-38
37°C

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#3 2011-01-02 15:38:49

STANKAR
Member
From: Czech Republic
Registered: 2010-11-16
Posts: 47

Re: help with cat

graysky wrote:
echo "/dev/sda: WDC WD1600BEVT-22ZCT0: 37°C" | cut -c34-38
37°C

Thanks a lot, i didn't know that is so simple.

Offline

#4 2011-01-02 16:54:20

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,543

Re: help with cat

echo "/dev/sda: WDC WD1600BEVT-22ZCT0: 37°C" | cut -d':' -f3

might be less brittle

Offline

#5 2011-01-02 17:11:51

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: help with cat

Sad cat is sad:
upsetcat.jpg

echo "/dev/sda: WDC WD1600BEVT-22ZCT0: 37°C" | awk '{ print $NF }'

Offline

#6 2011-01-02 17:22:59

STANKAR
Member
From: Czech Republic
Registered: 2010-11-16
Posts: 47

Re: help with cat

I found new problem. I didn't use this commands for x-lined output.

for example output of this: cat /proc/acpi/battery/BAT1/state

present:                 yes
capacity state:          ok
charging state:          charged
present rate:            298 mA
remaining capacity:      10364 mAh
present voltage:         12529 mV

If i need format output to "10364" or "charged" which command i can use ?

Offline

#7 2011-01-02 17:49:27

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: help with cat

This is getting repetitive...

awk '/^charging state/{ print $NF }' /proc/acpi/battery/BAT1/state
sed -n '/^charging state/s/.*:\(.*\)/\1/p' /proc/acpi/battery/BAT1/state
while IFS=':' read label val; do
  [[ $label == charging state ]] && { echo $val; exit; }
done < /proc/acpi/battery/BAT1/state

There's many ways to do these things. I advise you to learn a little on your own as it will make your life in linux much easier and more productive.
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://wiki.bash-hackers.org/doku.php

Last edited by falconindy (2011-01-02 17:50:00)

Offline

Board footer

Powered by FluxBB