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
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
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}'
> 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-...
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>
> 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
Many many thanks.....
Regards
Injam
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
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
> 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).
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
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
This produces only the line with "D", but does not produce the other three
lines with "C", "B" and "A", as the OP wanted.