Operating System - HP-UX
1834340 Members
1874 Online
110066 Solutions
New Discussion

Re: stripping parenthesized data from a file

 
SOLVED
Go to solution
Theresa Patrie
Regular Advisor

stripping parenthesized data from a file

Hi,
I have created a script (with much help from the forum gurus...thank you) that manipulates a netlist file. The following line is used to strip out junk data included in parenthesis.

inputfilename=$1
cat $inputfilename | sed -e '/^#/D'| sed 's/(.*)//'
Problem is that some of my netnames, which are enclosed in single quotes, also include parenthesis.

'address(10)' for example

How can I still strip out the junk perenthesized data with out stripping out parenthsized data enclosed in single quotes? Your help in this matter is greatly appreciated.
Theresa
This is my easy job!
13 REPLIES 13
Rodney Hills
Honored Contributor

Re: stripping parenthesized data from a file

How about-

perl -ne 'next if /^#/;/\'([^\']*)\'/ && print $1,"\n"' <$inputfilename

This will skip lines beginning with "#" and any line in single quotes will have the contents of the quotes extracted and printed

Sounds like you probabily have more to it than this...

HTH

-- Rod Hills
There be dragons...
Theresa Patrie
Regular Advisor

Re: stripping parenthesized data from a file

I see where you are going Rod, but I don't think it helps me do what I need. To be more specific, my netlist may look like this:

NET '/8250_D(0)' U5-2 U4-3 U2-10
NET '/8250_D(1)' U1-9 U2-2 U3-3 U4-4 U5-5 (NET_TYPE, "critical")
NET '/CLK' U1-10 U2-10 ($G, $P)

There can be any number of connections, there may or may not be parenthesis in the signal name and there may or may not be a parenthesized statement at the end of the line. I could probably add a $_ to your print command to get the rest of the line, but that still does not get rid of the paranthesized junk at the end. Any other thoughts??
Theresa
This is my easy job!
Rodney Hills
Honored Contributor

Re: stripping parenthesized data from a file

If you want to get rid of the parenthises stuff on the end too, then how about-

perl -ne 'next if /^#/;/\'([^\']*)\'([^(]*)/ && print "$1 $2\n"' <$inputfilename

This adds the text after the second quote (') up to (but not including the next left parenthesis).

HTH

-- Rod Hills
There be dragons...
Theresa Patrie
Regular Advisor

Re: stripping parenthesized data from a file

Ok Rod...I give. What the heck is that upside-down "V" and how do I create it?? Sorry for my ignorance!
Theresa
This is my easy job!
Theresa Patrie
Regular Advisor

Re: stripping parenthesized data from a file

Also...is there a good book out there that would help me with simple things like this without actually having to learn Perl completely. Once in a while I just need to do some simple data minipulation in my scripts and these perl one-liners usually work really well. But the syntax is quite cryptic. If I had a book to use as a reference that would actually explain the syntax used in the perl commands above, it would be a great help. Am I asking for too much?? Any input would be appreciated!
Thanks!
This is my easy job!
Theresa Patrie
Regular Advisor

Re: stripping parenthesized data from a file

Duh...nevermind!! OK, really, I am not that stupid...
I see now that it is a / followed by a \.
Theresa
This is my easy job!
Theresa Patrie
Regular Advisor

Re: stripping parenthesized data from a file

Hi Rod,
I am getting a syntax error saying
'(' unexpected
I tried a couple of minor changes, but cannot get rid of it. Could you help??
Thanks!
This is my easy job!
harry d brown jr
Honored Contributor

Re: stripping parenthesized data from a file

Just convert Rodneys' command line into a perl script:

#!/usr/bin/perl
while () {
if (!/^#/) {
/'([^']*)'([^\(]*)/ && print "$1---$2\n";
}
}

and execute as

./script < $inputfilename

live free or die
harry
Live Free or Die
Jean-Luc Oudart
Honored Contributor

Re: stripping parenthesized data from a file

Theresa,

non-perl solution.
I assume you only have one pair of single quote.

I did not print the quote but this can be done easily.

sed -e '/^#/D' | while read line
do
A1=$(echo $line | cut -f1 -d\' )
A2=$(echo $line | cut -f2 -d\' )
A3=`echo $line | cut -f3 -d\' | sed 's/(.*)//'`
echo $A1 $A2 $A3
done

Regards
Jean-Luc
fiat lux
Jean-Luc Oudart
Honored Contributor

Re: stripping parenthesized data from a file

Well (still same assumption)

this would be :
sed -e '/^#/D' | while read line
do
A1=$(echo $line | cut -f1,2 -d\' )
A3=`echo $line | cut -f3 -d\' | sed 's/(.*)//'`
echo $A1"'" $A3
done
fiat lux
Marvin Strong
Honored Contributor

Re: stripping parenthesized data from a file

As far as good books go.

Perl Programming
Perl Cookbook
Sed & Awk
Mastering Regular Expressions

The perl one liners are only cryptic because the options passed to perl on the command line, assume certain code like while loops etc..

If you do alot of data manipulation programming you should find all the above books helpful.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: stripping parenthesized data from a file

harry if you convert, make it better reading for her, not just expanded :)

--8<--- script
#!/opt/perl/bin/perl -n

/^#/ and next; # skip lines that start (^) with '#'

/ # start a match, this spans lines,
# because op the optional trailing 'x' flag. see later. it enables me to include comments
' # match a quote
( # start a capture to $1
[^']* # any character that is not a quote
# characters inside [] are a class/group.
# if it starts with a caret (^), the class is negated
# which means all character BUT the one's mentioned
# the star (*) means zero of more of the thing preceding the star
) # end of capture $1
' # a literal quote again
( # start of capture to $2
[^(]* # any number of non-open_paren
) # end of capture to $2
/x # end of match
and # if that matches
print "$1 $2\n"; # print the first capture followed by the second an a newline
-->8---

perl script <$inputfilename

Enjoy, Have FUN! H.Merijn [ who thinks that Rodney deserves 10 when you are going to assign points ]
Enjoy, Have FUN! H.Merijn
Theresa Patrie
Regular Advisor

Re: stripping parenthesized data from a file

Thank you to everyone who responded. I do have something that works perfectly for me now. This is what I ended up with...

#!/bin/sh
inputfilename=$1
## First delete all lines starting with "#"
cat $inputfilename | sed -e '/^#/D'|
## Now print out on a seperate line each pin_number and its signal.
awk '{sig=$2;for (i=3; i<=NF; i++){printf("%s %s\n",$i, sig);}}' |
## Delete any line starting with parenthesis
sed -e '/#(/D' |
## Print out all lines with a character, a digit, a dash then a digit.
perl -ne '/^(\D+)(\d+)-(\d+)(.*)/ && printf"%-2s%6d%6d%6d %s",$1,$2,$3,$4,$_' >junk

## First delete all lines starting with "#"
cat $inputfilename | sed -e '/^#/D'|
## Now print out on a seperate line each pin_number and its signal.
awk '{sig=$2;for (i=3; i<=NF; i++){printf("%s %s\n",$i, sig);}}' |
## Delete any line starting with parenthesis
sed -e '/#(/D' |
## Print out all lines with a character, a digit, a dash, a character then a digit.
perl -ne '/^(\D+)(\d+)-(\D+)(\d+)(.*)/ && printf"%-2s%6d%6s%6d %s",$1,$2,$3,$4,$_' >>junk

cat junk | sort | cut -c22- # Sort the list and cut off at U number.

Now, I know I am duplicating some work here, but I could not figure out how to deal with letters after the "-" easily. It was easier to do the processing twice and does not seem to have slowed anything down...it works in seconds.

The perl suggestions from Rodney and Harry were still not working exactly as I needed and I would have had to add my other processing into the perl script format. Since I already have them working with sed and awk, it was easier to tackle it this way. I just ended up leaving the parenthesized junk data alone until after it was put on its own line with the awk command. Once on its own line, the entire line gets deleted if it starts with a "(". Works like a charm.
Just FYI, I did try Jean-Luc's method and it does work, but boy is it ever slow. It took over 5 minutes to process a nelist the took only seconds with the script above.

Thanks for the description of the perl synatax, Procura. It is helping me to understand perl a little better.
This is my easy job!