Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

substring extraction

Original Message
Name: Niall21
Date: March 12, 2006 at 12:49:24 Pacific
Subject: substring extraction
OS: Unix Bourne shell
CPU/Ram: ?
Model/Manufacturer: ?
Comment:
I am trying to extract information from a string that is surrounded by parentheses:

line="blahblah(important)" #no spaces

I thought something like
echo ${line##(*)}
would work but it doesn't. It just prints out the comlpete string.

Could anyone please help?


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: March 12, 2006 at 13:13:29 Pacific
Subject: substring extraction
Reply: (edit)
Probably, some regex expert can come up with a better solution, but this one works:

#!/bin/ksh

line="blahblah(important) #no spaces"

newline=$(echo "$line" | cut -d\) -f1 | cut -d\( -f2)
echo $newline


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: March 13, 2006 at 09:13:15 Pacific
Subject: substring extraction
Reply: (edit)
I don't do much shell scripting so I'll show the perl syntax...the key part is the regex.

#!/usr/bin/perl -w

$line = q("blahblah(important) #no spaces");

print $1 if $line =~ /"(.+?)"/;


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: March 13, 2006 at 09:15:32 Pacific
Subject: substring extraction
Reply: (edit)
Oops, I did quotes, not parentheses.

Try this regex

/\((.+?)\)/


Report Offensive Follow Up For Removal

Response Number 4
Name: Niall21
Date: March 13, 2006 at 13:34:29 Pacific
Subject: substring extraction
Reply: (edit)
Cheers for the solution nails! You really hit the nail on the head! No pun intended :)
Thanks for the replies guys.

Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: substring extraction

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




My PC has been hijacked!

Lexmark 2600 Printer Issues

btk1w1 infected start here post

Unwanted message remians on screen

Slow boot time


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC