LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-18-2012, 03:35 AM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 79

Rep: Reputation: 0
Question 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..
 
Old 11-18-2012, 04:07 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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.
 
Old 11-18-2012, 04:09 AM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
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?
 
Old 11-18-2012, 04:13 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by khandu View Post
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?
 
Old 11-18-2012, 04:20 AM   #5
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
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..
 
Old 11-18-2012, 04:26 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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
 
1 members found this post helpful.
Old 11-18-2012, 04:34 AM   #7
khandu
Member
 
Registered: Sep 2003
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
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
 
  


Reply

Tags
grep, sed, terminal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on using sed to solve my problem sidw Linux - Newbie 4 04-20-2011 02:18 PM
Using Grep/Awk/Sed to get a substring from a command johnjust Programming 5 01-12-2010 09:02 PM
I am learning sed / grep working and am stuck with a problem chris_looks Linux - Newbie 12 08-14-2007 12:13 PM
I am learning sed / grep working and am stuck with a problem chris_looks Linux User Groups (LUG) 2 08-06-2007 02:31 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM


All times are GMT -5. The time now is 07:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration