LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 07-05-2009, 04:27 AM   #1
rsan
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Rep: Reputation: 0
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.
 
Old 07-05-2009, 05:05 AM   #2
rsan
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 07-05-2009, 05:08 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 07-05-2009, 05:25 AM   #4
rsan
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 07-05-2009, 05:26 AM   #5
rsan
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Re

with sed, awk many adhoc commands - none worked. any suggestions?
 
Old 07-05-2009, 06:00 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
awk '
{
 for(i=1;i<=2;i++){
    gsub(/.*\(|).*/,"",$i)
    print $i
 }
}' file
 
Old 07-05-2009, 06:01 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
reading path from a text file using bash script mksc Linux - Newbie 2 06-27-2008 03:25 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM


All times are GMT -5. The time now is 06:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration