Replace in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace in KSH
# 1  
Old 06-13-2011
Tools Replace in KSH

Hello,

i want to replace a string AAAA to BBB in a file using script.
File:
Code:
1
2
AAAA
3
4
5

Code:
old=AAAA
new=BBB
cat $file_name | while read line;
do
    if [ "$line" = "AAAA" ];then
       sed -e 's/$old/$new/'
    fi
done

HELP

Last edited by Franklin52; 06-13-2011 at 10:27 AM.. Reason: Correcting code tags
# 2  
Old 06-13-2011
Code:
awk -vo="AAAA" -vn="BBB" '{sub (o,n,$0)}1' $file_name

If you want to edit the file in place, then try:
Code:
perl -i -0pse 's/$o/$n/g' -- -o="AAAA" -n="BBB" $file_name

# 3  
Old 06-13-2011
No need to use a loop:
Code:
old=AAAA
new=BBB

sed "s/$old/$new/" $file_name > new_file

# 4  
Old 06-13-2011
Thanks for the reply but the file is being copied to the new file without changing the required field.
meaning file is the same.
I used :
Code:
sed 's/$old/$new/' $file_name > new_file


Last edited by Franklin52; 06-13-2011 at 10:42 AM.. Reason: Please use code tags
# 5  
Old 06-13-2011
Use double quotes:

Code:
sed "s/$old/$new/" $file_name > new_file

# 6  
Old 06-13-2011
When using double cthe new file is created with 0 kb size.
when using singal double quotes the file is created the same as the orig file.

---------- Post updated at 05:15 PM ---------- Previous update was at 04:44 PM ----------

Works.
Sorry.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to replace string in AIX ksh shell

My variable contains the following string I wish to replace \n with "space" so the expected output is: I understand that the /n is not a new linein this case. I'm on AIX using ksh shell. Below is all that I tried. echo $str | sed -e "s#\n# #g"; echo $str | sed -e "s#\n#' '#g";... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Replace in ksh

Dear All, I need you help, I have a file allfile_dump.txt \370380CoverPage124007001.pdf \370381CoverPage124007002.pdf \370382CoverPage124007003.pdf \370383CoverPage124007004.pdf \370384CoverPage124007005.pdf \370385CoverPage124007006.pdf \370386CoverPage124007007.pdf... (8 Replies)
Discussion started by: yadavricky
8 Replies

3. Shell Programming and Scripting

How to replace all ip addresses in all files in directory in ksh?

Hi All, I have a directory containing a lot of files and I want to search all files which contains IP addresses pattern and want to replace those ip addresses with equal number of, say, some character, 'x'. So after replacement the file should not contain any IP address. Like 10.122.53.5 ->... (3 Replies)
Discussion started by: sanzee007
3 Replies

4. Shell Programming and Scripting

ksh Remove and replace repeated in file

Hi, i need to read a line from a file and count the number of times it appear in, then continuous to the second line with the same. So when i count a line i have to remove all duplicates in the file to not count it another time. while read line do n=$(grep -c $line File) print "$line... (5 Replies)
Discussion started by: ToniX
5 Replies

5. Shell Programming and Scripting

Using sed in ksh to find and replace string

Hi All, I have a file in which contains location of various data files. I want to change locations using sed. Find and replace strings are in a separate file. Content of this file (/tmp/tt) - /dd/pp/test/test/1/ /pp/aa/test/dg1/ /dd/pp/test/test/2/ /pp/aa/test/dg2/ /dd/pp/test/test/3/... (2 Replies)
Discussion started by: pandeyra
2 Replies

6. Shell Programming and Scripting

Replace string in ksh

Hello, I want to locate a special character in each line of a file and replace it with another string that contains a special character and $i (i is incresing each cycle) string1: export IBAN=AAAAAAAAA . . . . export IBAN=zzzzzzzzzzz I want it to be: export IBAN=AAAAAAAAA . export... (1 Reply)
Discussion started by: LiorAmitai
1 Replies

7. Shell Programming and Scripting

Ksh: Replace backslash characters

Hi All, I have a requirement to read a line from a file with some search string, replace any backslash characters in that line and store in a variable. Shell script: replace.ksh #!/bin/bash file2=input.rtf line=`grep "Invoice Number" ${file2} | head -1 | sed 's/\\//g'` echo "start... (6 Replies)
Discussion started by: prashas_d
6 Replies

8. Shell Programming and Scripting

Find and Replace in ksh.

Hi Experts, I have a directory which have 500 ksh, they all have common path string. I have replaced all the ksh in different directory so i need to change the path string. Please help me how to search and replace it. Thanks SKG (1 Reply)
Discussion started by: gauravsunil
1 Replies

9. Shell Programming and Scripting

Find/replace to new file: ksh -> perl

I have korn shell script that genretaets 100 file based on template replacing the number. The template file is as below: $ cat template file number: NUMBER The shell script is as below: $ cat gen.sh #!/bin/ksh i=1; while ((i <= 100)); do sed "s/NUMBER/$i/" template > file_${i} ((... (1 Reply)
Discussion started by: McLan
1 Replies

10. Shell Programming and Scripting

Search and Replace in Ksh

All I need a code in Ksh to search and replace a string in a file. File A --- AAAA A BBBB B CCCC C DDDD E FFFF F File B: -------- AAAA BBBB CCCC DDDD (9 Replies)
Discussion started by: DeepakXavier
9 Replies
Login or Register to Ask a Question