Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-05-2009, 04:27 AM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 4
Rep:
|
Shell script:- Reading numbers embedded in brackets from a text file
Hi,
I need to extract two numbers (variable in length) from a fixed pattern in a text file.
The pattern is like this:
Word1(123456) Word2(678923) Word3(987)
Now from above pattern I need to simply extract 123456, 678923 and put them in two variables using a shell script (bash).
I exhausted all options including using sed, cut, awk but could not find a suitable answer.
Please help.
Thanks.
|
|
|
|
07-05-2009, 05:05 AM
|
#2
|
|
LQ Newbie
Registered: Jul 2009
Posts: 4
Original Poster
Rep:
|
I have got following working - but only managed to extract first number yet:
var=`cat test`
echo Readinf file
count=0
startCount=0
for readLine in $var; do
echo $readLine > tmprrs1
while [ ${readLine:count:1} ]
do
if [ ${readLine:count:1} = '(' ]
then
startCount=`expr $count + 2`
echo $startCount
fi
if [ ${readLine:count:1} = ')' ]
then
endCount=`expr $count`
echo $endCount
var1=`cut -c $startCount-$endCount tmprrs1`
echo "First var: " $var1
fi
#advance the counter to read the next character in the string i
count=`expr $count + 1`
done
done
|
|
|
|
07-05-2009, 05:08 AM
|
#3
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Hi,
Welcome to LQ!
Firstly: those are parentheses, not brackets.
And please show us what you've done e.g. with sed and awk,
since those are perfectly suitable tools for the job.
How are they failing for you?
Cheers,
Tink
|
|
|
|
07-05-2009, 05:25 AM
|
#4
|
|
LQ Newbie
Registered: Jul 2009
Posts: 4
Original Poster
Rep:
|
Re
Ok - this one I've done with following changes:
var=`cat test`
count=0
startCount=0
for readword in $var; do
echo $readword > tmprrs1
while [ ${readword:count:1} ]
do
if [ ${readword:count:1} = '(' ]
then
startCount=`expr $count + 2`
echo $startCount
fi
if [ ${readword:count:1} = ')' ]
then
endCount=`expr $count`
echo $endCount
var1=`cut -c $startCount-$endCount tmprrs1`
echo "First var: " $var1
fi
count=`expr $count + 1`
done
count=0
startCount=0
done
|
|
|
|
07-05-2009, 05:26 AM
|
#5
|
|
LQ Newbie
Registered: Jul 2009
Posts: 4
Original Poster
Rep:
|
Re
with sed, awk many adhoc commands - none worked. any suggestions?
|
|
|
|
07-05-2009, 06:00 AM
|
#6
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
awk '
{
for(i=1;i<=2;i++){
gsub(/.*\(|).*/,"",$i)
print $i
}
}' file
|
|
|
|
07-05-2009, 06:01 AM
|
#7
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
You did not show the attempts with sed and awk. If you can post them, we can tell you where the error was. In the meanwhile, you can be interested in reading http://www.grymoire.com/Unix/Sed.html
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:48 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|