The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk find/replace RobertSubnet Shell Programming and Scripting 2 03-08-2009 01:45 PM
Find and Replace in ksh. gauravsunil Shell Programming and Scripting 1 12-05-2008 07:55 AM
find and replace javeed7 Shell Programming and Scripting 1 04-02-2008 09:00 AM
find replace dbsurf Shell Programming and Scripting 2 01-25-2008 08:39 AM
find and replace mahabunta UNIX for Dummies Questions & Answers 7 09-21-2006 12:05 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-03-2009
Celvin VK Celvin VK is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 12
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.
  #2 (permalink)  
Old 04-03-2009
Celvin VK Celvin VK is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 12
i think this one works....
sed 's/\(_*\) *_./\1/' this will remove all the _s. I have to now get the replace

Quote:
Originally Posted by Celvin VK View Post
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.
  #3 (permalink)  
Old 04-03-2009
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Registered User
  
 

Join Date: Mar 2009
Location: Iran (Tehran)
Posts: 44
Quote:
Originally Posted by Celvin VK View Post
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.

Hello there,

I think that the following KornShell script does the job

Code:
#!/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:

Code:
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

Code:
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,
  #4 (permalink)  
Old 04-03-2009
Celvin VK Celvin VK is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 12
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...
Closed Thread

Bookmarks

Tags
bash, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0