LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 02:35 AM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

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, 03:07 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 03:09 AM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 93

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, 03:13 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 03:20 AM   #5
khandu
Member
 
Registered: Sep 2003
Posts: 93

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, 03:26 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 03:34 AM   #7
khandu
Member
 
Registered: Sep 2003
Posts: 93

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



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 01:18 PM
Using Grep/Awk/Sed to get a substring from a command johnjust Programming 5 01-12-2010 08:02 PM
I am learning sed / grep working and am stuck with a problem chris_looks Linux - Newbie 12 08-14-2007 11:13 AM
I am learning sed / grep working and am stuck with a problem chris_looks Linux User Groups (LUG) 2 08-06-2007 01:31 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 PM.

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
Open Source Consulting | Domain Registration