I have a file in which I have to truncate only the lines which comes between pattern @TEST and enabled="true">. When there is a match, the string between @TEST and enabled="true"> should be only 50 characters. All other lines should be left intact.
Example 1:
@TEST-TC_234 @TEST My name is Elli,I like to Travel, my hobbies are reading books,cooking,listening to music.I have a dog and the dog is black in colour enabled="true">
I have to change above line as shown below.
@TEST-TC_234 @TEST My name is Elli,I like to Travel, my hobbies are r enabled="true">
Also I have few sentences containing special characters which needs to be truncated as above
Example
@TEST 05030508227_${mode} @TEST 2 Framed ABCSubData (ABCupdateLoc) with abcdcsmelSubscriptionInfo parameter populated by tub and ABC DOS data, when ABCDN DOS & xyzabcdeDOS both use E.164 rule where the Restriction is '4-restrict via ABC DOS'" enabled="true">
I have to change above line as shown below.
awk
to the rescue!
$ awk '{if(match($0,/@TEST .*enabled="true">$/))
{bo=length("@TEST");
eo=length("enabled=\"true\">");
len=(RLENGTH-bo-eo)>50?50:RLENGTH-bo-eo;
print substr($0,0,RSTART+bo+len) substr($0,length($0)-eo)}
else print}' file
split the line for begin/end pattern if there is match; truncate the mid section and put them back together. If there is no match print unmodified lines.
For the updated example, the above script yields
@TEST 05030508227_${mode} @TEST 2 Framed ABCSubData (ABCupdateLoc) with abcdcsme enabled="true">