Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need Shell Script help

2 views
Skip to first unread message

Injam

unread,
Jul 1, 2008, 3:18:31 AM7/1/08
to
Hi,
I have the following file
swadmin@tb142:/rangedoms1/working/
CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> cat exp_aovr_send_new.dat
0001349000174P00000012D-ATPNB050062184TPNB050063880
0001349000174P60000329C-ATPNB050064199TPNB050064268

Now i need the above file in the below format i.e splitting up of
range character 23 to 25 in each record.
Resulting file
____________________
0001349000174P00000012DTPNB050062184TPNB050063880
0001349000174P00000012CTPNB050062184TPNB050063880
0001349000174P00000012BTPNB050062184TPNB050063880
0001349000174P00000012ATPNB050062184TPNB050063880
0001349000174P60000329CTPNB050064199TPNB050064268
0001349000174P60000329BTPNB050064199TPNB050064268
0001349000174P60000329ATPNB050064199TPNB050064268


Many Thanks
Injam

Loki Harfagr

unread,
Jul 1, 2008, 4:01:11 AM7/1/08
to
Tue, 01 Jul 2008 00:18:31 -0700, Injam did cat :

you could use char arrays operators, anyway I'd personnally use
the stream editor functions.

here's the father in sed:
$ sed 's/^\(.......................\)../\1/' yourfile
0001349000174P00000012DTPNB050062184TPNB050063880
0001349000174P60000329CTPNB050064199TPNB050064268


straight awk:
$ awk '{a=gensub(/^(.......................)../,"\\1","g",$0);print a} ' yourfile
0001349000174P00000012DTPNB050062184TPNB050063880
0001349000174P60000329CTPNB050064199TPNB050064268

using intervals in regexp:
$ awk --re-interval '{a=gensub(/^(.{23})../,"\\1","g",$0);print a} ' yourfile
0001349000174P00000012DTPNB050062184TPNB050063880
0001349000174P60000329CTPNB050064199TPNB050064268

Dave B

unread,
Jul 1, 2008, 4:39:47 AM7/1/08
to
Injam wrote:

You need to "expand", so to speak, the character sequence denoted by the
interval (eg, D-A, C-A, etc.), in reverse order. Generally speaking, doing
that in awk requires writing some small helper functions (see
http://www.gnu.org/software/gawk/manual/gawk.html#Ordinal-Functions). In
your specific case, since it seems you're always using uppercase letters, we
can probably take a shortcut. Try this:

awk 'BEGIN {s="ZYXWVUTSRQPONMLKJIHGFEDCBA"}

{p1=substr($0,1,22);p2=substr($0,26);
first=substr($0,23,1);last=substr($0,25,1);
i=1; while(substr(s,++i,1)!=first);
do {
print p1 substr(s,i,1) p2;
} while (substr(s,i++,1)!=last); }' yourfile

--
awk 'BEGIN{O="~"~"~";o="=="=="==";o+=+o;x=o""o;while(X++<x-o-O)c=c"%c";
X=O""O;printf c,O+x*o*o+X,(X+x)*(O+o)-o,+X*X-o-O,o+x*o*o+X,x*o*o+X-o-o,
x*(o+o)+X-O,+X*X-X+o+o,x+x+x-o,o+X+O+o+x*o*o,x+O+x*o*o,x*o*o+x+O+o+o+O,
x+o+x*o*o,x+x*o*o+O,o+x+x*o*o,o+X*o*o,X+x*o*o,x*o*o+O+x,x+x*o*o-O,X-O}'

Dave B

unread,
Jul 1, 2008, 4:42:52 AM7/1/08
to
Dave B wrote:

> i=1; while(substr(s,++i,1)!=first);

This should of course be

i=0; while(substr(s,++i,1)!=first);

i=1 will fail if you have something like Z-...

Injam

unread,
Jul 1, 2008, 6:22:14 AM7/1/08
to
On Jul 1, 1:39 pm, Dave B <da...@addr.invalid> wrote:
> Injam wrote:
> > Hi,
> > I have the following file
> > swadmin@tb142:/rangedoms1/working/
> > CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> cat exp_aovr_send_new.dat
> > 0001349000174P00000012D-ATPNB050062184TPNB050063880
> > 0001349000174P60000329C-ATPNB050064199TPNB050064268
>
> > Now i need the above file in the below format i.e splitting up of
> > range character 23 to 25 in each record.
> > Resulting file
> > ____________________
> > 0001349000174P00000012DTPNB050062184TPNB050063880
> > 0001349000174P00000012CTPNB050062184TPNB050063880
> > 0001349000174P00000012BTPNB050062184TPNB050063880
> > 0001349000174P00000012ATPNB050062184TPNB050063880
> > 0001349000174P60000329CTPNB050064199TPNB050064268
> > 0001349000174P60000329BTPNB050064199TPNB050064268
> > 0001349000174P60000329ATPNB050064199TPNB050064268
>
> You need to "expand", so to speak, the character sequence denoted by the
> interval (eg, D-A, C-A, etc.), in reverse order. Generally speaking, doing
> that in awk requires writing some small helper functions (seehttp://www.gnu.org/software/gawk/manual/gawk.html#Ordinal-Functions). In

> your specific case, since it seems you're always using uppercase letters, we
> can probably take a shortcut. Try this:
>
> awk 'BEGIN {s="ZYXWVUTSRQPONMLKJIHGFEDCBA"}
>
>            {p1=substr($0,1,22);p2=substr($0,26);
>             first=substr($0,23,1);last=substr($0,25,1);
>             i=1; while(substr(s,++i,1)!=first);
>             do {
>               print p1 substr(s,i,1) p2;
>             } while (substr(s,i++,1)!=last); }' yourfile
>
> --
> awk 'BEGIN{O="~"~"~";o="=="=="==";o+=+o;x=o""o;while(X++<x-o-O)c=c"%c";
> X=O""O;printf c,O+x*o*o+X,(X+x)*(O+o)-o,+X*X-o-O,o+x*o*o+X,x*o*o+X-o-o,
> x*(o+o)+X-O,+X*X-X+o+o,x+x+x-o,o+X+O+o+x*o*o,x+O+x*o*o,x*o*o+x+O+o+o+O,
> x+o+x*o*o,x+x*o*o+O,o+x+x*o*o,o+X*o*o,X+x*o*o,x*o*o+O+x,x+x*o*o-O,X-O}'- Hide quoted text -
>
> -show quoted text -

Hi,

I wrote the script that you have given here but i am getting error .
Please help me

swadmin@tb142:/rangedoms1/working/
CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> cat exp_aovr_send_new.dat

0001349000174P00000012B-ABATPNB050062184TPNB050063880
0001349000174P30000329B-ABATPNB050062184TPNB050063880
0001349000174P50000079C-BCBTPNB050562934TPNB050446531
0001349000174P60000079B-ABATPNB050062184TPNB050063880
0001349000174P60000329C-ACATPNB050064199TPNB050064268
0001349000174P80000079C-ACATPNB050064199TPNB050064268
swadmin@tb142:/rangedoms1/working/
CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> cat crstawk


awk ' BEGIN {s="ZYXWVUTSRQPONMLKJIHGFEDCBA"}
{
p1=substr($0,1,22);p2=substr($0,26);
first=substr($0,23,1);last=substr($0,25,1);

i=0; while(substr(s,++i,1)!=first);


do{
print p1 substr(s,i,1) p2;
} while (substr(s,i++,1)!=last);
}'

swadmin@tb142:/rangedoms1/working/
CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> awk -f crstawk
exp_aovr_send_new.dat
Syntax Error The source line is 1.
The error context is
awk >>> ' <<<
awk: 0602-500 Quitting The source line is 1.
swadmin@tb142:/rangedoms1/working/
CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV>


Dave B

unread,
Jul 1, 2008, 6:30:16 AM7/1/08
to
Injam wrote:

> Hi,
>
> I wrote the script that you have given here but i am getting error .
> Please help me
>
> swadmin@tb142:/rangedoms1/working/
> CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> cat exp_aovr_send_new.dat
> 0001349000174P00000012B-ABATPNB050062184TPNB050063880
> 0001349000174P30000329B-ABATPNB050062184TPNB050063880
> 0001349000174P50000079C-BCBTPNB050562934TPNB050446531
> 0001349000174P60000079B-ABATPNB050062184TPNB050063880
> 0001349000174P60000329C-ACATPNB050064199TPNB050064268
> 0001349000174P80000079C-ACATPNB050064199TPNB050064268
> swadmin@tb142:/rangedoms1/working/
> CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV> cat crstawk
> awk ' BEGIN {s="ZYXWVUTSRQPONMLKJIHGFEDCBA"}
> {
> p1=substr($0,1,22);p2=substr($0,26);
> first=substr($0,23,1);last=substr($0,25,1);
> i=0; while(substr(s,++i,1)!=first);
> do{
> print p1 substr(s,i,1) p2;
> } while (substr(s,i++,1)!=last);
> }'

If you put the script in a file, you have to remove the call to awk. Put the
following in your file:


BEGIN {s="ZYXWVUTSRQPONMLKJIHGFEDCBA"}
{
p1=substr($0,1,22);p2=substr($0,26);
first=substr($0,23,1);last=substr($0,25,1);
i=0; while(substr(s,++i,1)!=first);
do{
print p1 substr(s,i,1) p2;
} while (substr(s,i++,1)!=last);
}

Then call awk with

awk -f scriptfile yourfile

Injam

unread,
Jul 1, 2008, 6:35:36 AM7/1/08
to
> x+o+x*o*o,x+x*o*o+O,o+x+x*o*o,o+X*o*o,X+x*o*o,x*o*o+O+x,x+x*o*o-O,X-O}'- Hide quoted text -
>
> - Show quoted text -

Many many thanks.....


Regards
Injam

Loki Harfagr

unread,
Jul 1, 2008, 7:05:51 AM7/1/08
to
Tue, 01 Jul 2008 08:01:11 +0000, Loki Harfagr did cat :

BTW, in case all what your script must do is the transformation
you described here's an even simpler line:
$ awk --re-interval '{print gensub(/^(.{23})../,"\\1","")} ' yourfile

Injam

unread,
Jul 2, 2008, 4:44:43 AM7/2/08
to
> CRST_OVERLAY_ENHANCE_Analysis_RNGCTRL_DEV>- Hide quoted text -

>
> - Show quoted text -


Hi ,

Your answers are very useful for me as i am new to unix scripting.
Here i have one more question...how to combine the splitted lines to
original one?

Many thanks
Injam

Dave B

unread,
Jul 2, 2008, 5:35:56 AM7/2/08
to
Injam wrote:

> Hi ,
>
> Your answers are very useful for me as i am new to unix scripting.
> Here i have one more question...how to combine the splitted lines to
> original one?

If you mean doing the reverse operation, then you can do this:

awk '(p1=substr($0,1,22))!=last {
if (last) {print p1 b "-" e p2}
p2=substr($0,24); b=substr($0,23,1)
}
{e=substr($0,23,1);last=p1}
END {print p1 b "-" e p2}' yourfile

Note that the above program assumes that the first 22 characters are enough
to identify a distinct run of data. This means that it will fail if the
input has something like (I put the added spaces for clarity)

0001349000174P00000012 D TPNB050062184TPNB050063880
0001349000174P00000012 C TPNB050062184TPNB050063880
0001349000174P00000012 B TPNB050062184TPNB050063880
0001349000174P00000012 A TPNB050062184TPNB050063880

0001349000174P00000012 C TPNB050064199TPNB050064268
0001349000174P00000012 B TPNB050064199TPNB050064268
0001349000174P00000012 A TPNB050064199TPNB050064268

If this is not what you mean, or the above solution does not work for you,
post an example (input and expected output).

Loki Harfagr

unread,
Jul 2, 2008, 9:37:47 AM7/2/08
to
Wed, 02 Jul 2008 01:44:43 -0700, Injam did cat :


Is this what you mean?

Your "splitted lines" file:
$ cat fixedfields3


0001349000174P00000012DTPNB050062184TPNB050063880
0001349000174P00000012CTPNB050062184TPNB050063880
0001349000174P00000012BTPNB050062184TPNB050063880
0001349000174P00000012ATPNB050062184TPNB050063880
0001349000174P60000329CTPNB050064199TPNB050064268
0001349000174P60000329BTPNB050064199TPNB050064268
0001349000174P60000329ATPNB050064199TPNB050064268

the re-combination:
$ awk --re-interval '{print gensub(/^(.{23})/,"\\1-A","")} ' fixedfields3
0001349000174P00000012D-ATPNB050062184TPNB050063880
0001349000174P00000012C-ATPNB050062184TPNB050063880
0001349000174P00000012B-ATPNB050062184TPNB050063880
0001349000174P00000012A-ATPNB050062184TPNB050063880
0001349000174P60000329C-ATPNB050064199TPNB050064268
0001349000174P60000329B-ATPNB050064199TPNB050064268
0001349000174P60000329A-ATPNB050064199TPNB050064268

Sashi

unread,
Jul 22, 2008, 5:28:31 PM7/22/08
to

At the risk of incurring disapproving frowns from the men in grey
beards:
$ echo 0001349000174P00000012D-ATPNB050062184TPNB050063880 | cut -
c1-23,26-
0001349000174P00000012DTPNB050062184TPNB050063880

Unless you're stuck with an awk implementation for this problem, cut
seems to be a simpler (and more appropriate) tool for this task.
Sashi

pk

unread,
Jul 22, 2008, 5:37:13 PM7/22/08
to

This produces only the line with "D", but does not produce the other three
lines with "C", "B" and "A", as the OP wanted.

0 new messages