LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using cut (http://www.linuxquestions.org/questions/linux-newbie-8/using-cut-869101/)

athrin 03-17-2011 12:48 AM

using cut
 
i want to cut
MemTotal: 254788 kB
into
254788
can someone help me with that

corp769 03-17-2011 12:50 AM

Code:

echo "MemTotal: 254788 kB" | cut -c 11-16
That should do it. Replace whatever command need be in place of echo. Note: I am on windows right now at work, so you might have to edit the numbers to get the correct output.

Cheers,

Josh

Telengard 03-17-2011 12:56 AM

I would use

Code:

~$ echo 'MemTotal: 254788 kB' | cut -d ' ' -f 2
254788
~$

This way it works even if the length of the string changes.

athrin 03-17-2011 12:57 AM

aaa.. i want to cut it without using echo.
if i use
cat /proc/meminfo | grep MemTotal | cut -d : -f2
254788 kB will shown and i also want to remove the kB

corp769 03-17-2011 12:58 AM

Quote:

Originally Posted by Telengard (Post 4293431)
I would use

Code:

~$ echo 'MemTotal: 254788 kB' | cut -d ' ' -f 2
254788
~$

This way it works even if the length of the string changes.

I didn't know that, thanks.

Telengard 03-17-2011 01:11 AM

Quote:

Originally Posted by athrin (Post 4293433)
aaa.. i want to cut it without using echo.
if i use
cat /proc/meminfo | grep MemTotal | cut -d : -f2
254788 kB will shown and i also want to remove the kB

I see why. cut isn't very intelligent about field separators. Try this instead:

Code:

~$ cat /proc/meminfo | awk '/MemTotal/ {print $2}'
1033828
~$

Quote:

Originally Posted by corp769 (Post 4293436)
I didn't know that, thanks.

You're welcome of course.

athrin 03-17-2011 01:13 AM

Quote:

Originally Posted by Telengard (Post 4293445)
I see why. cut isn't very intelligent about field separators. Try this instead:

Code:

~$ cat /proc/meminfo | awk '/MemTotal/ {print $2}'
1033828
~$



You're welcome of course.

thanks for your help

Telengard 03-17-2011 01:18 AM

Quote:

Originally Posted by athrin (Post 4293446)
thanks for your help

You're very welcome. I'm just glad to know you found my response helpful.
;)


All times are GMT -5. The time now is 08:06 PM.