Parsing out strings for repeating delimiters for everyline

Hello:

I have some text output, on SunOS 5.11 platform using KSH:

I am trying to parse out each string within the () for each line.

I tried, as example:

perl -lanF"[()']" -e 'print "$F[1] $F[2] $F[3] $F[4] $F[5] $F[6]"'

But for some reason, the output gets all garbled after the the first fields.
Guess I can try the following but it is very messy, as I would have to do that for each Descriptor before the first (.
The number of fields can change dynamically..

Another example:

cat $FILE | |nawk -FDESCR '{print $2}'| perl -lanF"[()]" -e 'print $F[1]'

So the desired output woul be:

Not sure what else I can try.

Thanking you for any advice !!

Here is an approach using nawk by checking each character:-

nawk '
        {
                for ( i = 1; i <= length; i++ )
                {
                        c = substr($0,i,1)

                        if ( c == "(" )
                                flag = 1

                        if ( flag && c != "(" && c != ")" )
                                printf c

                        if ( c == ")" )
                                flag = 0
                }
                printf "\n"
        }
' file
1 Like

Why switch between perl and awk ? Stick to one:

awk -F"[()]" '{for (i=2; i<=NF; i+=2) printf "%s ", $i; printf RS}' file
BOS.FOS.T.CDG.MEDGTOC.01 CLENTT1 NOTFIXED YES Cargo dangerous goods queue CLCRGT1 BOS.FOS.T.CDG.MEDGTOC.01   
CLFOST1 CLENTT1 NOTFIXED YES Qmgr Alias for CLFOST1 Cluster   
RPT.PSS.T.VILS.ODY.01 CLENTT1 YES PSS MSGS TO VILS ODYSSEY CLITAT1 RPT.PSS.T.VILS.ODY.01   

AND:

EDIT: or

sed 's/^[^(]*(\|)[^(]*(\|[^)]*) *$/ /g' file
 BOS.FOS.T.CDG.MEDGTOC.01 CLENTT1 NOTFIXED YES Cargo dangerous goods queue CLCRGT1 BOS.FOS.T.CDG.MEDGTOC.01  
 CLFOST1 CLENTT1 NOTFIXED YES Qmgr Alias for CLFOST1 Cluster  
 RPT.PSS.T.VILS.ODY.01 CLENTT1 YES PSS MSGS TO VILS ODYSSEY CLITAT1 RPT.PSS.T.VILS.ODY.01  
1 Like

Thats working ..

Thank you !!

one more question:

How to get only the header strings:

From:

to:

Tried this but not working::

nawk -F"[)(]" '{for (i=2; i<=NF; i+=2) printf "%s ", $i; printf RS}' file

Thnx again !!

Of course not - that was meant to extract the strings within parentheses, and you shouldn't expect it to do the opposite. You need to modify it slightly:

awk -F"[()]" '{for (i=1; i<=NF; i+=2) printf "%s ", $i; printf RS}' file
QREMOTE  CLUSTER  DEFBIND  DEFPSIST  DESCR  RQMNAME  RNAME  XMITQ  

Gonna try to understand the syntax better.

Thank you ..

So further to this discussion, would there be a way to delete one complete field ??
For example, if wanting to remove the DESCR() field.

From:

To:

Tried the sed command:

sed 's/DESCR.*) R//'

But this strips the first char "R" next field, returning: NAME(BOS.FOS.T.CDG.MEDGTOC.01) XMITQ( )

Thnx again !!

echo 'QREMOTE(BOS.FOS.T.CDG.MEDGTOC.01) CLUSTER(CLENTT1) DEFBIND(NOTFIXED) DEFPSIST(YES) DESCR(Cargo dangerous goods queue) RQMNAME(CLCRGT1) RNAME(BOS.FOS.T.CDG.MEDGTOC.01) XMITQ( )' | sed 's/DESCR[^)]*)//g'
Moderator comments were removed during original forum migration.