Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove text after specific character

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I have a script that pulls info from a file and populates it into a database. However one of these lines, can sometimes put too much into the field. For example, I could have lines that look like this, the result of doing a du -sh on a file: (not in the same file though)
Code:
688M /srv/[URL unfurl="true"]www/htdocs/mysite.tgz[/URL]
12G /srv/[URL unfurl="true"]www/htdocs/my2site.tgz[/URL]

The problem i am having is I need enough room in my field for 688M so I cannot change the field size. What is happening is my field ends up like this:
Code:
688M
12G /

What I want to do is remove all characters following the 'M' or the 'G'.

I am relatively new to PERL so not sure what function to use or how to do it. Anyone able to lead me in the right direction?
 
Hi

What are you asking for :
Code:
  [blue]DB<1>[/blue] $str='688M /srv/[URL unfurl="true"]www/htdocs/mysite.tgz'[/URL]

  [blue]DB<2>[/blue] $str=~s/([MG]).*/\1/

  [blue]DB<3>[/blue] print $str
688M
What sounds more reasonable :
Code:
  [blue]DB<1>[/blue] $str='688M /srv/[URL unfurl="true"]www/htdocs/mysite.tgz'[/URL]

  [blue]DB<2>[/blue] $str=~s/ .*//

  [blue]DB<3>[/blue] print $str
688M
What most of us would probably do :
Code:
  [blue]DB<1>[/blue] $str='688M /srv/[URL unfurl="true"]www/htdocs/mysite.tgz'[/URL]

  [blue]DB<2>[/blue] @field=split / /,$str         

  [blue]DB<3>[/blue] print $field[0]
688M


Feherke.
 
Thanks for your reply, I ended up using the split function to do it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top