1834208 Members
2459 Online
110066 Solutions
New Discussion

Script ideas??

 
SOLVED
Go to solution
Russ Hancock_1
Frequent Advisor

Script ideas??

I need to extract information from lines in a file that are contained within any number of square brackets...

i.e. INPUT returns OUTPUT
XXXXX[A]XXXXX returns A
XYXYX[ABC]XYX returns ABC
ZXCZX[A][B]ZC returns AB
etc...

Does anyone have any ideas please....
Cheers
Russ
Russ
13 REPLIES 13
Steven E. Protter
Exalted Contributor

Re: Script ideas??

I might suggest a start.

awk using [ and or ] to strip the data into a variable

var1=$(awk[ 'print $1')

This is just a course of action, the code doesn't work.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: Script ideas??

# perl -nle'@x=/\[([^]]*)\]/g and print@x' file
A
ABC
AB
#

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
RAC_1
Honored Contributor

Re: Script ideas??

cat input_your_file|tr "\[" ":"|tr "\]" ":"|awk -F : '{print $2}'

Anil
There is no substitute to HARDWORK
H.Merijn Brand (procura
Honored Contributor

Re: Script ideas??

RAC, that will only print the first instance in each line

and it also uses cat where absolutely not neccecary. Useless waste of process space. Use input redirection instead:

NOT # cat file | process
BUT # process < file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Kent Ostby
Honored Contributor

Re: Script ideas??

Create a file called s.awk and put this into it:

{usestring=$0;printstring="";
for (idx1=1;idx1 { usestring=substr(usestring,idx1);
check1=index(usestring,"[");
check2=index(usestring,"]");
if (check1>0)
{use2=substr(usestring,check1+1,check2-check1-1);
printstring=printstring use2; idx1=check2+1;
}
else
{idx1=length($0);}
}
print printstring;
}

Then run:

awk -f s.awk < input > output

I'm sure I could stream line it, but it will work as it is now.

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
RAC_1
Honored Contributor

Re: Script ideas??

Thanks procura.

I am learing perl from gurus like you.

And yes car=t was required. I did not give that much thought to it. Was very fast.

anil
There is no substitute to HARDWORK
Jean-Luc Oudart
Honored Contributor
Solution

Re: Script ideas??

Cannot compete with Merijn, but an awk solution :

awk '{
i2=index($0,"]");
str=$0;
while(i2 > 0) {
i1=index(str,"[");
printf("%s",substr(str,i1 + 1,i2 - i1 -1));
str=substr(str,i2 + 1, length(str));
i2=index(str,"]");
}
printf("\n");
} '

Regards,
Jean-Luc
fiat lux
Russ Hancock_1
Frequent Advisor

Re: Script ideas??

Thanks Jean-Luc I used your script...
Russ
H.Merijn Brand (procura
Honored Contributor

Re: Script ideas??

May I ask why you prefer the use several lines of unmaintainable code over a very short and easy perl line of code?

Do you need explaining of any part?

In the original part you did not state what script language you wanted.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Elmar P. Kolkman
Honored Contributor

Re: Script ideas??

There is more to it... Jean-Luc's code will fail if you have lines containing closing square brackets, but without opening brackets, you will get those lines too.

But if are sure the brackets are always matched, you could also do it this way:

awk '/\[/ { sub("^[^\[]*\[","");
sub("\][^\[]*$","");
sub("\][^\[]*\[","");
print
}'
Same could be done with sed and some replacements using sed.
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: Script ideas??

Sorry, forgot one char/thing... Last sub should be a gsub, because you can have more then 2 blocks in square brackets.

But I still think Procura's solution is better and on large files faster.
Every problem has at least one solution. Only some solutions are harder to find.
Jean-Luc Oudart
Honored Contributor

Re: Script ideas??

Elmar,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is more to it... Jean-Luc's code will fail if you have lines containing closing square brackets, but without opening brackets, you will get those lines too.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The code is based on the sample from the poster.

Merijn,
No offence, I don't think my code is unmaintainable. Certainly, it takes more than one line, and if don't speak perl, that's where you cannot maintain it , would you agree ?

Regards,
Jean-Luc
fiat lux
H.Merijn Brand (procura
Honored Contributor

Re: Script ideas??

Jean-Luc, no offence at all, and sure, it is maintainable, but so verbose that typo's are just a matter of time.

IMHO my perl solution is sooooo darn simple that it just is the best solution. No offence to anyone, and I know I sometimes post rather obfuscated solutions, but this one is so simple that I cannot understand why someone would choose any other solution.

There is a right tool for every problem, and I'm not stating that tool is Perl all the time, but here it outperforms any other in simplicity and shortness

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn