find and replace and keep

Hi All

I've file in which has these lines in it

create fil23456 read on 3345
create fil23456_1 read on 34567
create fil23456_2 read on 36789

I'm trying to replace the lines in such a way that in the end the file will look like

create fil23456 read on 3345
alter fil23456 read on 34567
alter fil23456 read on 36789

I would like to replace the occurrences of _1, _2 (this can be many so i cant hardcode saying _1 and _2)

I tried some of the sed, but couldnt succed (it is removing _ but not the 1s and 2s, then other one is reomving everything before _)

Can someone please help.

i think this one works....
sed 's/\(*\) *./\1/' this will remove all the _s. I have to now get the replace :slight_smile:

Hello there,

I think that the following KornShell script does the job

#!/bin/ksh

function substring
{
    TOKEN=$1
    LENGTH=${#TOKEN}
    COUNTER=0
    typeset -L1 CURRENT_CHARACTER
    RESULT=""
    
    while (( COUNTER < LENGTH ))
    do
        CURRENT_CHARACTER=$TOKEN
        if [[ $CURRENT_CHARACTER = "_" ]]
        then
            if (( (COUNTER + 1) < LENGTH ))
            then
                if [[ ${TOKEN#?} = [0-9] ]]
                then
                    print "$RESULT"
                fi
            else
                RESULT="$RESULT$CURRENT_CHARACTER"
            fi
        else
            RESULT="$RESULT$CURRENT_CHARACTER"
        fi
        (( COUNTER = COUNTER + 1 ))
        TOKEN=${TOKEN#?}
    done
}

INPUT_FILE=$1
OUTPUT_FILE=$1_temp

if [[ -a $OUTPUT_FILE ]]
then
    rm $OUTPUT_FILE
fi

cat $INPUT_FILE > $OUTPUT_FILE


while read LINE
do
    for TOKEN in $LINE
    do
        if [[ $TOKEN = *_[0-9] ]]
        then
            print -n "$(substring $TOKEN) "
        else
            print -n "$TOKEN "
        fi
    done
    print ""
done < $INPUT_FILE > $OUTPUT_FILE

I tested it with the following input file:

create fil23456 read on 3345
create fil23456_1 read on 34567
create fil23456_2 read on 36789
create fil11111_ read on 36789
create fil22222_5 read on 36789

Which provided the following putput

create fil23456 read on 3345 
create fil23456 read on 34567 
create fil23456 read on 36789 
create fil11111_ read on 36789 
create fil22222 read on 36789

Regards,
:slight_smile:

Atlast i got suceeded with this
sed '/:./s/^/#/g;
s/\(
\) _./\1/;
s/#create filS
./alter filS/g'
Input is this
create fil23456 read on 3345
create fil23456_1 read on 34567
create fil23456_2 read on 36789
create fil26756 read on 56890
create fil26756_1 read on 37897
create fil26756_2 read on 67667
and this is the output
create fil23456 read on 3345
alter filS3456 read on 34567
alter filS3456 read on 36789
create fil26756 read on 56890
alter filS6756 read on 37897
alter filS6756 read on 67667
Thanks a lot for all your help...