Find & Replace with same case letters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find & Replace with same case letters
# 1  
Old 11-21-2014
Find & Replace with same case letters

I have text with upper and lower case words. I want to find something and replace it with something new. But it should match the case - Meaning - it should replace old upper cased word with NEW upper case word and lower with lower.

example:
Code:
this text is very simple TEXT.

now I want to replace both cases of "text" with word to match with case.
output should be
Code:
this word is very simple WORD.

text is replace with word and TEXT with WORD.

Is there a way to do this in sed or some other command - without the use of a loop.
Thanks in Advance
# 2  
Old 11-21-2014
Whenever you replace something in a text file you either have to memory map the whole in C, make changes, then close the file. Or. Loop through the original file processing line by line and write the new file. In Linux
Code:
sed -i

will do this - update in place. (It does the loop behind closed curtains). Other UNIXES, no.

We need:
Your shell and your unix OS to go further.

And it would be nice to know why loops are out of the question. Sounds misguided to me.
# 3  
Old 11-21-2014
Your requirements aren't very clear.

What should the output be if your input is:
Code:
this text is very simple TEXT in the context of a TEXTILE mill.
This Text is in a mixed case ConTeXt.

when you're replacing text with word?

Will the original text to be replaced always contain the same number of characters as the replacement text? If not, what should happen with the text:
Code:
ThIs iS.

when you want to change is to eater?
# 4  
Old 11-21-2014
Hi Jim - Here is my OS:

Code:
$>uname -o
GNU/Linux

And I am trying to reduce my lines in my code, hoping that awk or sed has something to take care of case even for the replace code.

Don - it should change all occurrences of "text/TEXT" and change it to the appropriate "word/WORD". Mixed case is not in my file and would never be there, so it doesn't matter in case of mixed case.
its like global replace, but it has to take care of cases too:
text should be replaced with word
TEXT should be replace with WORD
TeXt should be replaced with WoRd (not critical)
find string and replace string will always have same number of characters.

Hope I am clear now.
# 5  
Old 11-21-2014
So maybe you want something like:
Code:
#!/bin/ksh
awk -v from="${1:-text}" -v to="${2:-word}" '
BEGIN {	lf = tolower(from)
	uf = toupper(from)
	lt = tolower(to)
	ut = toupper(to)
}
{	gsub(lf, lt)
	gsub(uf, ut)
}
1'

If you save this in a file (for example tester) and make it executable with:
Code:
chmod +x tester

then you can invoke it with:
Code:
./tester from_string to_string < input_file > output_file

If you invoke it without operands it will default to text and word, respectively. This will not work reliably if any characters in the 1st operand have a special meaning in an extended regular expression (but for alphanumeric strings this is not an issue). This will not modify mixed case input, but you said that is not an issue.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 6  
Old 11-24-2014
Thanks for your time Don. Really appreciate your help.

Last edited by grep_me; 11-24-2014 at 04:42 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find & Replace

Hi I am looking to rename the contents of this dir, each one with a new timestamp, interval of a second for each so it the existing format is on lhs and what I want is to rename each of these to what is on rhs..hopefully it nake sense CDR.20060505.150006.gb CDR.20121211.191500.gb... (3 Replies)
Discussion started by: rob171171
3 Replies

2. Shell Programming and Scripting

Error while executing switch case for find and replace specific file. Help Me...

case "$inputs" in sapte) find /pools/home_unix/sapte/work/models/model -name "*.xml" exec rm -i {} \;; ckm1) find /pools/home_unix/ckm1/work/models/model -name "*.xml" exec rm -i {} \;; I am getting error like as below. ./menu1.sh: line 144: syntax error near unexpected token `)'... (4 Replies)
Discussion started by: lathigara
4 Replies

3. UNIX for Dummies Questions & Answers

GREP Find & Replace <p class>

I am making an eBook. I am editing the html in BBedit. I need to replace all <p class="s5"> with just a <p>. How do I write this for GREP? Thank you, Abby (5 Replies)
Discussion started by: cuddlykitty
5 Replies

4. Shell Programming and Scripting

Find & replace --> create a new file

Hi All, I have a unix shell script file as below. My task is a)to replace 248 to 350 and need to create a new file as BW3_350.sh b)to replace 248 to 380 and need to create a new file as BW3_380.sh c)to replace 248 to 320 and need to create a new file as BW3_320.sh there is no... (6 Replies)
Discussion started by: karthi_mrkg
6 Replies

5. Shell Programming and Scripting

Replace lower case letters with N

Hi, I have a string of letters that are upper and lower case. I would like to replace all the lowercase letters with N. Also, there are only 4 letters, so it may be easier to replace each lower case letter with N. Either way, I do not know know to do this. Example line ... (6 Replies)
Discussion started by: mikey11415
6 Replies

6. Shell Programming and Scripting

PHP Compare 2 Arrays find difference & case insensitive

Hi, I need an elegant solotion in php. I need to compare 2 arrays (array1 & array2), to find all instances of array 2 which is not in array1. I don't want to see any instances of array1 wich is not in array2 (here "the") Array1: This, is, the, data, of, array1 Array2: this, is, data, Of,... (2 Replies)
Discussion started by: lowmaster
2 Replies

7. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

8. Shell Programming and Scripting

find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like... Host cmg-iqdrw3p4 LocalForward *:9043 localhost:9043 Host cmg-iqdro3p3a LocalForward *:10000 localhost:10000 Host cmg-iqdro3p3b LocalForward... (2 Replies)
Discussion started by: treadwm
2 Replies

9. UNIX for Dummies Questions & Answers

improving my script (find & replace)

Hi all, I have a script that scan files, find old templet and replace it with new one. #!/bin/ksh file_name=$1 old_templet=$2 new_templet=$3 # Loop through every file like this for file in file_name do cat $file | sed "s/old_templet/new_templet/g" > $file.new #do a global searce and... (8 Replies)
Discussion started by: amir_yosha
8 Replies

10. Shell Programming and Scripting

Find & Replace

I get a text file with 70+ columns (seperated by Tab) and about 10000 rows. The 58th Column is all numbers. But sometimes 58th columns has "/xxx=##" after the numeric data. I want to truncate this string using the script. Any Ideas...:confused: (3 Replies)
Discussion started by: gagansharma
3 Replies
Login or Register to Ask a Question