Replace string with asterisk(*) in variable


 
Thread Tools Search this Thread
Operating Systems AIX Replace string with asterisk(*) in variable
# 1  
Old 04-19-2010
Replace string with asterisk(*) in variable

I was trying to replace a string ( for eg - @@asterisk@@ to * ) in variable using

Code:
cat $INFILE  | while read LINE
do
    stmt1=`echo $LINE | sed 's/@@asterisk@@/\*/g'`
    stmt=$stmt' '$stmt1
    stmt2=`echo $LINE`
    STATEMENT=$STATEMENT' '$stmt2
done
echo 'Statement with sed -- > ' $stmt 
echo 'Statement without sed -- > ' $STATEMENT

Output I am getting is
=================
Code:
Statement with sed -- >
Statement without sed -- >  INSERT INTO table1 SELECT @@ASTERISK@@ FROM table2

Seems sed is not working.. Appreciate if someone say what is wrong in this.

Thanks in advance

Last edited by Scott; 04-19-2010 at 01:29 PM.. Reason: Code tags, please...
# 2  
Old 04-19-2010
Quote:
Originally Posted by Vaddadi
Seems sed is not working.. Appreciate if someone say what is wrong in this.
Actually nothing is wrong in your replacement, but you fail to protect your variables content until the output:

Code:
variable_with_glob='*'
print - "compare this: $variable_with_glob"
print - "to this:" $variable_with_glob

I hope this helps.

bakunin
# 3  
Old 04-19-2010
Or do something with turning off glob
ksh turns off globbing with set -f or set noglob
Code:
#!/bin/ksh
cat $INFILE | while read LINE 
do 
   set -f
   stmt1=`echo $LINE | sed 's/@@asterisk@@/\*/g'` 
   stmt=$stmt' '$stmt1 stmt2=`echo $LINE` 
   STATEMENT=$STATEMENT ' ' $stmt2 
   set +f
done
print "$STATEMENT"

Make sure you turn globbing back on - it will affect subsequent code. Ver badly -- if it is off.
# 4  
Old 04-19-2010
Thanks ... tried set -f & set +f , but it didn't work ..
# 5  
Old 04-19-2010
It looks like you have this in your input file:
Code:
INSERT INTO table1 SELECT @@ASTERISK@@ FROM table2

Your sed command is looking for "@@asterisk@@" instead of "@@ASTERISK@@". Is that the problem?

Also, make sure you use double-quotes on the last two echos like this or the "*" will be replaced with a list of files in the current directory.
Code:
echo "Statement with sed -- >  $stmt "
echo "Statement without sed -- >  $STATEMENT"



---------- Post updated at 02:28 PM ---------- Previous update was at 02:23 PM ----------

This is what I get now with your script. I named it "test" with the INFILE=test.in

Code:
host:/home:$ ls -l test*
-rwxr-xr-x    1 root     system          267 Apr 19 14:25 test
-rw-r--r--    1 root     system           51 Apr 19 14:24 test.in

host:/home:$ cat ./test.in
INSERT INTO table1 SELECT @@ASTERISK@@ FROM table2

host:/home:$ cat ./test
INFILE=./test.in
cat $INFILE | while read LINE
do
        stmt1=`echo $LINE | sed 's/@@ASTERISK@@/\*/g'`
        stmt=$stmt' '$stmt1
        stmt2=`echo $LINE`
        STATEMENT=$STATEMENT' '$stmt2
done
echo "Statement with sed -- >  $stmt "
echo "Statement without sed -- >  $STATEMENT"

host:/home:$ ./test
Statement with sed -- >   INSERT INTO table1 SELECT * FROM table2
Statement without sed -- >   INSERT INTO table1 SELECT @@ASTERISK@@ FROM table2

host:/home:$

# 6  
Old 04-20-2010
Thanks to all ... it worked like a champ.

Shyam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace string in variable?

Hello, I have simple while and for loops in a shell script and I would like to replace some characters in COL2 when I run it. I am on ubuntu 14.04 while read COL1 COL2 COL3 COL4 do name=$COL2 #cat $name | sed -i "s|_| |g" $name for i in $COL3 $COL4 do some codes ...... run $i b="$name"... (11 Replies)
Discussion started by: baris35
11 Replies

2. Shell Programming and Scripting

Replace substring from a string variable

Hi, Wish to remove "DR-" from the string variable (var). var="DR-SERVER1" var=`echo $var | sed -e 's/DR-//g'` echo "$var" Expected Output: However, I get the below error: Can you please suggest. (4 Replies)
Discussion started by: mohtashims
4 Replies

3. UNIX for Dummies Questions & Answers

Replace variable string with text

Hi All, Hoping someone can help.... I am trying to work out how I can ammend a log file to remove variable strings in order to remove confidential information which I cant pass on. As an example I have used phone numbers. A large log file contains multiple lines containing something like the... (6 Replies)
Discussion started by: mutley2202
6 Replies

4. Shell Programming and Scripting

Need to use asterisk in variable

Hi All, I am having a challange to pass the asterisk in the variable. Basically, I am writing a shell script to check if a marker file exists but when I am assigning the varialbe it cannot use the wildcard asterisk as expected, therefore, my program is always outputs "Marker file is not... (4 Replies)
Discussion started by: express14
4 Replies

5. Shell Programming and Scripting

How to replace a string to asterisk?

Hi, I would like to ask for you idea on how to convert a string to "*". say password and username. I have a shell script that will promt the user to enter username and password. This will be passed to sqlplus command. echo "Please enter user name" read USERNAME echo... (2 Replies)
Discussion started by: reignangel2003
2 Replies

6. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

7. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

8. Shell Programming and Scripting

Replace string in file with a variable value

Hi Fellows, I am new to shell, please help we me out in this.. i have file which some lines like this.. $$param1='12-jan-2011' $$param2='14-jan-2011' $$param3='30-jan-2011' . . .....so on.. I want to change $$param3 to '31-dec-2011'. i have variable which is storing(30-jan-2011 this... (1 Reply)
Discussion started by: victor369
1 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

Replace string in a file w/ a variable value

I am trying to replace the default home page for several mac user accounts, I wrote a script that will hunt the files down and replace them with a pre-configured set. The problem I am having is that the download destination path for the browser is hard coded into a .plist (text config file) file... (5 Replies)
Discussion started by: tret
5 Replies
Login or Register to Ask a Question