LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 08-29-2007, 03:33 PM   #1
doug.mm0bja
LQ Newbie
 
Registered: Aug 2007
Posts: 3

Rep: Reputation: 0
Analysing string containing metacharacters


Trying to parse file lines to find position of opening and closing brackets so that a number value 1 to 90 can be extracted from the brackets.
Each line is similar to:
portalarm(1)=15

The script below is an initial attempt to find the opening bracket but it generates the error:

./question: line 12: [: -ne: unary operator expected
expr: syntax error

Any pointers to detecting the () appreciated.

Thank you,
Doug


#!/bin/bash
while read LINE
do
a="("
car=`expr match "$LINE" "$a"`
echo $car
if [ $car -ne 0 ]; then
echo $LINE
fi
done < gb3ke515ch1
 
Old 08-29-2007, 04:26 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,335
Blog Entries: 55

Rep: Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535
a="\("...............
 
Old 08-29-2007, 04:40 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Simple but clear! Furthermore, the expr match construct is not what you need, since it searches for matching substring at the beginning of the string. Use expr index instead.
A more straightforward way to do this task could be (probably among many other)
Code:
echo $LINE | grep -o \([0-9]\) | tr -d \(\)
He he... I know it is not so simple if you are not experienced, but can be a good exercise!
 
Old 08-29-2007, 05:03 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,335
Blog Entries: 55

Rep: Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535
Quote:
Originally Posted by colucix View Post
Furthermore, the expr match construct is not what you need
Good one, I should have mentioned that since I use 'index' by reflex.


Quote:
Originally Posted by colucix View Post
A more straightforward way to do this task could be (probably among many other)
Here's one without 'grep' and 'tr'. If you want to see it expanded do a "set -x" before running this:
Code:
n="0"; until [ $n -eq ${#i} ]; do [ "${i:$n:1}" = "(" ] && { echo "$n"; break; } || ((n++)); done
 
Old 08-29-2007, 05:58 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Pure bash! I like it!
 
  


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
Analysing network traffic jayeola Linux - Networking 2 06-28-2006 01:30 PM
metacharacters "The Pipe command mikeinanson Linux - Newbie 4 03-11-2006 12:36 PM
analysing alsa captured data morph_ind Programming 3 08-05-2005 11:21 AM
Need help analysing lspci output benjaminrtz Linux - Newbie 1 03-16-2005 03:54 PM
please tell me about some circuit analysing(simulating) software? randika Linux - Newbie 4 11-18-2004 02:40 AM


All times are GMT -5. The time now is 07:05 PM.

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