UNIX Scripting
Hi I am trying to read a record and split it into multiple records
My Record looks like this
1001A0010@B0010*&^0)C0012hgdj&6sD0020fhfri93kivmepi9
where
UniqueID is 1001
segments are A,B,C,D
length of each segment is 4 characters after the segment 0010 for A
0010 for B 0012 for C etc
I have to split this file into
1001A0010@
B0010*&^0)
C0012hgdj&6s
D0020fhfri93kivmepi9
and Then I will be appending the Unique ID to the other segments like
1001A0010@
1001B0010*&^0)
1001C0012hgdj&6s
1001D0020fhfri93kivmepi9
Can anyone help me how to read a line and cut it and write it out
I wanna loop the line until end of line
Can anyone guide me how to do this
Thanks
Sreekanth
This is one way to do it...
I'm sure someone will have a shorter code than this.
while read record
do
uniq_id=`echo $record | awk -F'A' '{print $1}'`
part1=`echo $record | awk -F'B' '{print $1}'`
part2=`echo $record | awk -F'C' '{print $1}' | awk -F'B' '{print "B" $2}'`
part3=`echo $record | awk -F'D' '{print $1}' | awk -F'C' '{print "C" $2}'`
part4=`echo $record | awk -F'D' '{print "D" $2}'`
echo $part1
echo $uniq_id$part2
echo $uniq_id$part3
echo $uniq_id$part4
echo "******************************************"
done < test1.txt
In perl you could do something like
$str = "1001A0010@B0010*&^0)C0012hgdj&6sD0020fhfri93kivmepi9";
$str =~ /(.*)A(.*)B(.*)C(.*)D(.*)/;
$uni = $1;
$a = $2;
$b = $3;
$c = $4;
$d = $5;
$seg1 = $uni.A.$a;
$seg2 = $uni.B.$b;
$seg3 = $uni.C.$c;
$seg4 = $uni.D.$d;
and so on.
No I cannot split the record based on the A. I have to look at the Segement A and then increase it by length then find next Segment (It can be any B,C,D,E,F) then split it until end of line
I have to loop the line until end of line and split the line based on length
The segments are variable ....
Is this a school project ?
If the length of the Segment is fixed -> 4 characters, than why do you need to have the rest of the junk in the output
eg:
1001A0010@
B0010*&^0)
echo "1001A0010@B0010*&^0)C0012hgdj&6sD0020fhfri93kivmepi9" |
awk '{
Id=substr($0,1,4)
n=split($0,m,/[A-Z]/)
for(i=2;i<=n;++i) {
if (cnt)
cnt+=l+1
else
cnt=length(Id)+1
printf("%s%s%s\n",Id,substr($0,cnt,1),m)
l=length(m)
}
}'