# Remove everything before... in a string



## IT_Architect (Jun 2, 2012)

I read a string in a file with the command you see below
[cmd=]egrep -i "Volume.Capacity_datastore1:" /tmp/vmware_api_10.17.127.132.txt[/cmd]
The string it returns is:

```
Volume.Capacity_datastore1:     122406567936
```
Then I added this:
[cmd=]egrep -i "Volume.Capacity_datastore1:" /tmp/vmware_api_10.17.127.132.txt | sed 's/[[:space:]]//g'[/cmd]
and now the string it returns is:

```
Volume.Capacity_datastore1:122406567936
```

How can I remove from the beginning of the line through the ":"?

*Edit*:  I guess I had to post here to figure it out.  The answer is:
[cmd=]egrep -i "Volume.Capacity_datastore1:" /tmp/vmware_api_10.17.127.132.txt | sed 's/[[:space:]]//g' | sed 's/^.*://g'[/cmd]


----------



## wblock@ (Jun 3, 2012)

IT_Architect said:
			
		

> How can I remove from the beginning of the line through the ":"?
> 
> *Edit*:  I guess I had to post here to figure it out.  The answer is:
> [cmd=]egrep -i "Volume.Capacity_datastore1:" /tmp/vmware_api_10.17.127.132.txt | sed 's/[[:space:]]//g' | sed 's/^.*://g'[/cmd]



Removing the spaces separately is not necessary.  .* matches any character, so the second sed(1) command that deletes everything from the start of the string to the colon is all that's needed.  Beware of regular expression "greediness":

```
% echo "   abcdef:123:ghi" | sed 's/^.*://g'
ghi
```

The regex is greedy and takes the biggest match it can find.


----------



## UNIXgod (Jun 3, 2012)

cut() can also work. as with wblocks echo statement:


```
% echo "   abcdef:123:ghi" | cut -d ':' -f3
ghi
```

Both ways are appropriate. Even using tr() and awk() could work. The cut command would take the least memory out of all the solutions.


----------



## IT_Architect (Jun 4, 2012)

Both replies were a HUGE help.  They both helped me get a handle on working with streams, sed and cut.  One good example is worth a 1000 man pages.  LOL!  I couldn't use them only because they didn't trim the leading spaces in front of 122406567936, but they helped me in my next hurdle tremendously.

Thanks!


----------



## wblock@ (Jun 4, 2012)

If you know for sure that there will be only spaces and not other whitespace like a tab in front of the numbers, sed(1) can handle that:
`% echo "Volume.Capacity_datastore1:     122406567936" | sed -e 's/^.*:\( \)*//'`

If there could be tabs, Perl's regex handling is far, far more powerful than sed(1)'s 1972 view of the world:
`% echo "Volume.Capacity_datastore1:     122406567936" | perl -pe 's/^.*:(\s)*//'`

\s means whitespace, including tabs.


----------



## IT_Architect (Jun 4, 2012)

wblock@ said:
			
		

> If you know for sure that there will be only spaces and not other whitespace like a tab in front of the numbers...If there could be tabs, Perl's regex handling is far, far more powerful


"When the only tool you know is a hammer, everything looks like a nail."  I'm VERY open to your suggestion because, as you already know, I'm at least as likely to get tabs as spaces in the output of a machine.  I know PHP but nothing about Perl.  I'm not great with regex, but I know enough to convert POSIX to PCRE to make PHP scripts compatible for PHP 5.3+.

Thanks!


----------



## IT_Architect (Jun 6, 2012)

wblock@ said:
			
		

> ...sed(1) can handle that...


Something that has been puzzling for a long time and I've been too embarrassed to ask is, what is the significance of the 1 in sed(1)?  I've seen things like that for years, and sometimes it's an 8 etc.  :q


----------



## wblock@ (Jun 6, 2012)

There are multiple sections for man pages that apply to different categories.  For example, section 4 is generally device drivers.  See the "intro" pages: intro(1), intro(2), intro(3), all the way up to intro(9).

So sed(1) is the man page for sed from the general commands, tools and utilities section.

There usually isn't a lot of overlap in names, so the section number is often left out:
`% man sed`

But consider re.  Is that "re" meaning Perl regular expressions, or "re" the Realtek device driver?
`% man 3 re`
`% man 4 re`


----------



## IT_Architect (Jun 6, 2012)

My mystery has been solved.  Thanks tons!


----------



## DutchDaemon (Jun 6, 2012)

This is why we have these forums tags (written out):


[man=1]sed[/man]
[man=8]ifconfig[/man]


which show up as:
sed(1)
ifconfig(8)


----------



## UNIXgod (Jun 7, 2012)

wblock@ said:
			
		

> If you know for sure that there will be only spaces and not other whitespace like a tab in front of the numbers, sed(1) can handle that:
> `% echo "Volume.Capacity_datastore1:     122406567936" | sed -e 's/^.*:\( \)*//'`
> 
> If there could be tabs, Perl's regex handling is far, far more powerful than sed(1)'s 1972 view of the world:
> ...



tr(1) can also be used in conjunct with cut(1) as so:
`% echo "Volume.Capacity_datastore1:     122406567936" | cut -d':' -f2  | tr -d ' '`

If you need whitespace and tabs tr can do both as well by adding a space before or after \t in this example it's prefixed:
`% echo "Volume.Capacity_datastore1:\t\t\t\t  122406567936" | cut -d':' -f2  | tr -d ' \t'`
POSIX way has [:blank:] which is both whitespace and tabs which would be `% tr -d '[:blank:]'`

tr(1) is pretty cool. It's actually one of the older programs that dates back to the multics project.



			
				IT_Architect said:
			
		

> Both replies were a HUGE help.  They both helped me get a handle on working with streams, sed and cut.  One good example is worth a 1000 man pages.  LOL!  I couldn't use them only because they didn't trim the leading spaces in front of 122406567936, but they helped me in my next hurdle tremendously.
> 
> Thanks!



Don't forget to hit the thanks button once in awhile.


----------



## IT_Architect (Jun 7, 2012)

UNIXgod said:
			
		

> Don't forget to hit the thanks button once in awhile.


Thanks button?  Where?


----------



## UNIXgod (Jun 7, 2012)

IT_Architect said:
			
		

> Thanks button?  Where?



It's a button with the thumbs up icon on the lower right side of the post

Image looks like this:


----------



## IT_Architect (Jun 7, 2012)

UNIXgod said:
			
		

> It's a button with the thumbs up icon on the lower right side of the post


Got it! Those types of graphics are not readable on all screens.

Thanks!


----------

