Help understanding sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help understanding sed
# 1  
Old 10-01-2009
Help understanding sed

I am trying to create a basic script that converts an Oracle script into a Sybase script.

The only things im changing are Datatypes and the to_char and to_date functions.

I am not really 100% sure of the way it works. I have tried running the functions through a loop to replace each word line by line but that didnt work.

If you could help me understand what I am doing wrong I would be very grateful.

Code:
#!/bin/bash

function menu()
{

echo "This program will convert Oracle syntax into SYBASE syntax."

}

function replaceDate()
{

cat $file | sed 's/TO_DATE/CONVERT/g' >> $newFile
cat $file | sed 's/TO_CHAR/CONVERT/g' >> $newFile
}
#Datatype conversion
function replaceWord()
{
cat $file | sed 's/VARCHAR2/VARCHAR/g' >> $newFile
cat $file | sed 's/NUMBER/NUMERIC/g' >> $newFile
cat $file | sed 's/DATE/DATETIME/g' >> $newFile

}
menu

read -p "Please enter the file name that you wish to convert: " file
read -p "Please enter the name of the new file: " newFile


        replaceDate
        replaceWord

Thanks.
# 2  
Old 10-01-2009
Hi.

You're reading the original file every time, writing the original file every time appended to the newfile with the changes.

Should perhaps be:
Code:
function replaceDate()
{
sed 's/TO_DATE/CONVERT/g' $file > $newFile
sed 's/TO_CHAR/CONVERT/g' $newfile > $file
rm $newfile
}
#Datatype conversion
function replaceWord()
{
sed 's/VARCHAR2/VARCHAR/g' $file > $newFile
sed 's/NUMBER/NUMERIC/g' $newfile > $file.$$
sed 's/DATE/DATETIME/g' $file.$$ > $file
rm $newfile $file.$$
}


And you don't need cat. sed is capable of reading its own files.

Of course if you don't want to overwrite the original file, copy it before you call the function.

Last edited by Scott; 10-01-2009 at 12:05 PM..
# 3  
Old 10-01-2009
It still doesnt seem to work, it is in fact only changing the TO_DATE and TO_CHAR functions, but none of the others.

Code:
#!/bin/bash

function replaceDate()
{
sed 's/TO_DATE/CONVERT/g' $file >> $newFile
sed 's/TO_CHAR/CONVERT/g' $newfile>> $newFile
}

#Datatype conversion
function replaceWord()
{
sed 's/VARCHAR2/VARCHAR/g' $file >> $newFile
sed 's/NUMBER/NUMERIC/g' $newfile >> $newFile2
sed 's/DATE/DATETIME/g' $newFile2 >> $file

}
function menu()
{

echo "This program will convert Oracle schemas into SYBASE schemas."

}


menu

read -p "Please enter the file name that you wish to convert?" file
read -p "Please enter the name of the new file?" newFile


        replaceDate
        replaceWord

Anybody have any idea why?
Also, if I just had one global sed statement, would it subsitute all of the chosen word in the file, or would it be just on the line?

Thanks.
# 4  
Old 10-01-2009
You didn't exactly follow what I said.

You don't append to the output file, otherwise all you do it end up with the file twice in the one file.

and don't use $newfile >> $newfile.

Use a different filename, or use the sed "in-place" replacement (which I never use, so check the man page for sed).

And yes. A single sed statement would make more sense.

either
Code:
sed -e "s/SOMETHING/SOMETHING_ELSE/g;s/ANOTHER_YETANOTHER/g" $file > $newfile
or
sed -e "s/SOMETHING/SOMETHING_ELSE/g" -e "s/ANOTHER_YETANOTHER/g" $file > $newfile

# 5  
Old 10-01-2009
Sorry I did do what you said, but it didnt work, so I fiddled about with it, so I posted the wrong code last post.

This was the code when I tried your suggestion. But it still wouldnt work.

Code:
#!/bin/bash



function replaceDate()
{
sed 's/TO_DATE/CONVERT/g' $file >> $newFile
sed 's/TO_CHAR/CONVERT/g' $newfile>> $file
}

#Datatype conversion
function replaceWord()
{
sed 's/VARCHAR2/VARCHAR/g' $file >> $newFile
sed 's/NUMBER/NUMERIC/g' $newfile >> $file.$$
sed 's/DATE/DATETIME/g' $file.$$ >> $file
rm $newFile $file.$$
}
function menu()
{

echo "This program will convert Oracle schemas into SYBASE schemas."

}


menu

read -p "Please enter the file name that you wish to convert?" file
read -p "Please enter the name of the new file?" newFile


        replaceDate
        replaceWord

# 6  
Old 10-01-2009
That's still not what I said (unless you copied it about 10 seconds after I wrote it! - which I guess you did) Smilie

I never used >>, only >

In any case, your own suggestion to use a single sed is cleaner and easier.

Code:
#!/bin/bash
function menu()
{
  echo "This program will convert Oracle schemas into SYBASE schemas."
}
 
menu
read -p "Please enter the file name that you wish to convert?" file
read -p "Please enter the name of the new file?" newFile
 
[ ! -f ${file:-$$} -o -z "$newfile" ] && echo "Source file doesn't exist or target not specified" && exit 1

sed "s/TO_DATE/CONVERT/g;s/TO_CHAR/CONVERT/g;s/VARCHAR2/VARCHAR/g;s/NUMBER/NUMERIC/g;s/DATE/DATETIME/g" $file > $newfile


Last edited by Scott; 10-01-2009 at 04:00 PM.. Reason: Minor correction
# 7  
Old 10-01-2009
Thank you. That works perfectly!

Sorry I missread the >> appends!

Thanks for all your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Quick help on Understanding sed Regex

Hi Guys, Could you please kindly explain what exactly the below SED command will do ? I am quite confused and i assumed that, sed 's/*$/ /' 1. It will remove tab and extra spaces .. with single space. The issue is if it is removing tab then it should be Î right .. please assist.... (3 Replies)
Discussion started by: Nandy
3 Replies

2. Shell Programming and Scripting

Need your help in understanding this

Hi, I found this in a script and I would like to know how this works Code is here: # var1=PART1_PART2 # var2=${var1##*_} # echo $var2 PART2 I'm wondering how ##* makes the Shell to understand to pick up the last value from the given. (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

3. Shell Programming and Scripting

Understanding sed

Hi, can some one suggest me,how "sed" is managed to delete the second field here. Any explanation on , how the below code is working would be appreciated. sed 's/^\(*\)::/\1::/' /etc/passwd sed 's/*:/:/2' /etc/passwd (14 Replies)
Discussion started by: panyam
14 Replies

4. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

5. UNIX for Dummies Questions & Answers

understanding sed command

Hi Friends, I need a small help in understanding the below sed command. $ cat t4.txt 1 root 1 58 0 888K 368K sleep 4:06 0.00% init 1 root 1 58 0 888K 368K sleep 4:06 0.00% init last $ sed 's/*$//' t4.txt 1 root 1 58 0 888K ... (3 Replies)
Discussion started by: forroughuse
3 Replies

6. UNIX for Dummies Questions & Answers

understanding {%/*}/

Hi Gurus: I am trying to understand the following line of code.I did enough of googling to understand but no luck.Please help me understand the follow chunk of code: X=$0 MOD=${X%/*}/env.ksh X is the current script from which I am trying to execute. Say if X=test.ksh $MOD is echoing :... (3 Replies)
Discussion started by: vemana
3 Replies

7. Shell Programming and Scripting

need help understanding mv

I just started shell coding and I'm a bit confused on how 'mv' works can someone explain to me how it works and if i did this correctly. Thanks. echo "Enter Name of the first file:" read file1 #echo $file1 if ; then echo "Sorry, file does not exist." exit 1 ... (16 Replies)
Discussion started by: taiL
16 Replies

8. Shell Programming and Scripting

understanding the sed command

Guys, I am trying to understand the sed command here. adx001 $ a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil adx001 $ b=`echo $a | sed 's/\//\\\\\//g'` adx001 $ echo $b \/clocal\/dctrdata\/user\/dctrdat1\/trdroot\/recouncil The sed command i took it from the script. Please... (3 Replies)
Discussion started by: mac4rfree
3 Replies

9. UNIX for Advanced & Expert Users

need further understanding of init.d

I needt o know how what init.d does and how it knows which dameons/applications to turn off and how to restart the applications after reboot. any OS - solaris/hp-ux (1 Reply)
Discussion started by: jigarlakhani
1 Replies
Login or Register to Ask a Question