Hi Unix Mates,
I have requirement where in which I have to replace second to last -1 occurence of space with a word and last occurence of a space with a word and a space,input would be a file.
Desired result:
Second consecutive to last-1 occurence of space replace with: HRU
LAST consecutive occurence : WRU[SPACE]
input
cat myfile
Wow what a[8spaces]wonderful feeling to be alive
<Code>
Wow what a HRUHRUHRUHRUHRUHRUHRUWRU wonderful WRU feeling to be alive
Thanks iN ADVANCE!!!
Hello Kingcobra,
Following may help you in same.
echo "Wow what a wonderful feeling to be alive" | awk '{for(i=1;i<=NF;i++){if(i==4){print "HRUHRUHRUHRUHRUHRUHRUWRU"} {if(i==5){print "WRU"}} {if(i!=4 || i!=5){print $i}}}}' ORS=" "
Output will be as follows.
Wow what a HRUHRUHRUHRUHRUHRUHRUWRU wonderful WRU feeling to be alive
Thanks,
R. Singh
@Kingcobra: could you elaborate what a consecutive space means to you. Is your input file is incorrect? What have you tried?
--
@Ravindersingh that is not a solution...
Thanks mates.
Actaullt my spaces got supressed when I have put it inside code tag
Help required is
First space --->Do nothing
Second consecutive space--->REPLACE WITH HBU
third space --->REPLACE WITH HBU
n-1th consecutive space ----> REPLACE WITH HBU
Last space in consecutive sequence--->REPLACE WITH WBU[space]
Thanks Scrutinizer,Singh for your help:b:
Try:
sed -e 's/\( *\) /\1WRU /g' -e :a -e 's/ \([HW]RU\)/ HRU\1/;ta' file
I believe there should be six HRU's (2nd until last -1) not 7, correct?
Bulls eye!!! Scrutinizer you are an amazing guy.Proud to be in this forum