LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need a particular sed / grep command to solve problem (https://www.linuxquestions.org/questions/programming-9/need-a-particular-sed-grep-command-to-solve-problem-4175437649/)

khandu 11-18-2012 02:35 AM

Need a particular sed / grep command to solve problem
 
I am running some command in terminal.. and getting the following result

Code:

      VRAM (Total): 1024 MB
NOTE the gap in the starting..

Now I really want to create two commands.

1) remove the gap from start and give the result.. so result should be

Quote:

VRAM (Total): 1024 MB
and

2) Remove starting text entirely and just give

Quote:

1024 MB
Help please.. I am just playing around with geektool in mac and want to grab this part as a result..

catkin 11-18-2012 03:07 AM

A pure bash solution:
Code:

#!/bin/bash
shopt -s extglob  # Enable extended globbing
var='      VRAM (Total): 1024 MB'
var2=${var##*( )}  # The *( ... ) requires extended globbing
echo ">$var2<"
var3=${var#*: }
echo ">$var3<"

Extended globbing is explained here, at the end of the section.

khandu 11-18-2012 03:09 AM

Quote:

Originally Posted by catkin (Post 4831841)
A pure bash solution:
Code:

#!/bin/bash
shopt -s extglob  # Enable extended globbing
var='      VRAM (Total): 1024 MB'
var2=${var##*( )}  # The *( ... ) requires extended globbing
echo ">$var2<"
var3=${var#*: }
echo ">$var3<"

Extended globbing is explained here, at the end of the section.

thanks.. but forgot to mention..

it needs to be a one line terminal solution.. as I am grepping this in a command.. I want to extend the command to get the desired result.. i guess need some kind of regex to do this?

catkin 11-18-2012 03:13 AM

Quote:

Originally Posted by khandu (Post 4831843)
I want to extend the command to get the desired result.

In the OP you suggest you want two results. Do you actually want only the last result?

khandu 11-18-2012 03:20 AM

actually need two seperate results.. not in same command.. sorry for the confusion

so one command will give me 1 and second will give me 2..

so I guess I am looking for two seperate grep / sed commands..

catkin 11-18-2012 03:26 AM

Code:

c@CW9:~$ echo '      VRAM (Total): 1024 MB' | sed 's/^ *//'
VRAM (Total): 1024 MB
c@CW9:~$ echo '      VRAM (Total): 1024 MB' | sed 's/.*: //'
1024 MB


khandu 11-18-2012 03:34 AM

Quote:

Originally Posted by catkin (Post 4831854)
Code:

c@CW9:~$ echo '      VRAM (Total): 1024 MB' | sed 's/^ *//'
VRAM (Total): 1024 MB
c@CW9:~$ echo '      VRAM (Total): 1024 MB' | sed 's/.*: //'
1024 MB


Thanks :)


All times are GMT -5. The time now is 06:41 AM.