Visit Our UNIX and Linux User Community


find and replace variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace variable
# 1  
Old 08-08-2007
find and replace variable

Is there an easy way of doing this

cat file1

Code:
jkasjhjgfg LTRIM(RTRIM(aa_bb_cde))
aragsfdg LTRIM(RTRIM(aa_bb_cde)) aregfafdgfg
sdgsfdagdfg  gadfg
eafgsadgsa
asdgsfdgag LTRIM(RTRIM(aa_bb_cde)) rfghsdfhd

I want to replace each occurence of LTRIM(RTRIM($x)) with
LENGTH(LTRIM(RTRIM($x)))=0

So my output file should be

Code:
jkasjhjgfg LENGTH(LTRIM(RTRIM(aa_bb_cde)))=0
aragsfdg LENGTH(LTRIM(RTRIM(aa_bb_cde)))=0 aregfafdgfg
sdgsfdagdfg  gadfg
eafgsadgsa
asdgsfdgag LENGTH(LTRIM(RTRIM(aa_bb_cde)))=0 rfghsdfhd

# 2  
Old 08-08-2007
Code:
sed 's/\(LTRIM(RTRIM[^)]*))\)/LENGTH(\1)=0/' input_file


Last edited by Shell_Life; 08-08-2007 at 12:53 PM.. Reason: Assure no ')' after it.
# 3  
Old 08-08-2007
Works...thanks Shell!
# 4  
Old 08-09-2007
sed

try this oneSmilie

Code:
sed 's/\(LTRIM(RTRIM(\)\(.*\)\())\)/LENGTH(\1\2\3)=0/' filename

# 5  
Old 08-09-2007
Quote:
Originally Posted by summer_cherry
try this oneSmilie
Code:
sed 's/\(LTRIM(RTRIM(\)\(.*\)\())\)/LENGTH(\1\2\3)=0/' filename

Summer_cherry,
Your solution works fine for the given data sample.

Knowing how the posts are here, there is a strong possibility that
the OP could come back to say the solution is not working for another
situation in the file.

Your code will break for the following scenario:
Code:
jkasjhjgfg LTRIM(RTRIM(aa_bb_cde)) abcdef LENGTH(SQRT(aa_bb_cde))


Previous Thread | Next Thread
Test Your Knowledge in Computers #876
Difficulty: Medium
All operational code must be POSIX compliant in order to be cost effective.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and replace with variable -sed

Hi, I have a ksh script where I am trying to mask the password in the log files. $loc - is my directory $PGUIDE_DB_USER_PSW - is a variable that holds the password I am looking for find $loc/logs -type f -exec sed -i "s/$PGUIDE_DB_USER_PSW/*****/"g {} \; I get an error: ... (2 Replies)
Discussion started by: amitlib
2 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

Find a variable in a file and replace its value

HI , I can't find a solution to the following: In a simple menu script I want to capture the input from the user with "read" and use it as a value in a variable Rempages="some_value" which is in a different script file. So I have to perform a search and replace for this variable and only... (4 Replies)
Discussion started by: svetoslav_sj
4 Replies

4. Shell Programming and Scripting

Find and replace string from file which contains variable and path - SH

e.g. /home/$USER/.config replace it with "" (empty) Is this possible? I think you should play a bit with sharps ## and sed:b: (2 Replies)
Discussion started by: hakermania
2 Replies

5. Shell Programming and Scripting

Find and replace with variable using sed or awk

hi, i have file say email.temp looks like Bell_BB 17 Bell_MONTHLY 888 SOLO_UNBEATABLE 721 and another file r3 Bell BB,Bell_BB Bell,Bell_MONTHLY SOLO,SOLO_UNBEATABLE i want email.temp files $1 say Bell_BB should be replaced by r3 Bell BB and Bell_MONTHLY by... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

6. Shell Programming and Scripting

How to partially replace variable value?

I have variable $2 whose value is expdp_SDW_MSTR_VMDB.par I want to replace three characters from right (par) with (log) Input --> expdp_SDW_MSTR_VMDB.par Output --> expdp_SDW_MSTR_VMDB.log Thanks Deep (2 Replies)
Discussion started by: deep.singh
2 Replies

7. Shell Programming and Scripting

How to replace variable inside the variable

hi sir, i need your help for this script inside /rnmucdr/ednms05/ken/xMNBDF045_Script.sql content variable like this select * from invoice where bill_date=$BILLDATE and startNum=$STARTPARTNNUM and total_partn=$TOTALPARTN if i just paste this replace with the $SCRIPT it works great,if... (31 Replies)
Discussion started by: mani_um
31 Replies

8. Shell Programming and Scripting

Replace variable with a user defined variable

I have a file that has a list of entries in a column x z z z x y z The column can have any length and any number of any strings. I need to replace each unique string with a user defined number. I can filter the unique entries out using awk '{if (NF==5) print $2}' file | uniq | nl >... (1 Reply)
Discussion started by: ce124
1 Replies

9. Shell Programming and Scripting

Find and replace the value in a variable

I have a variable whose value is I="user1:x:1100:1200:ID for user1:/home/user1:/bin/ssh-dummy-shell" I want to replace the last part '/bin/ssh-dummy-shell' with '/bin/true' I tried using sed but it garbled sed 's/\/bin\/ssh-dummy-shell/\/bin\/true' $I Thanks for the help (5 Replies)
Discussion started by: aajmani
5 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies

Featured Tech Videos