Shell Scripting , need to search and replace a string in AIX

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell Scripting , need to search and replace a string in AIX
# 1  
Old 09-04-2016
Shell Scripting , need to search and replace a string in AIX

Hi Guys,

I need to search and replace a string in AIX using variables and should be case insensitive.

I am able to search and replace using below command but its not working as case insensitive.

Code:
cat abc.txt | sed -e 's/$a/$b/g'  >  abc.txt

But i need to perform this with case insensitive , it should ignore upper/lower case.

I have tried below code but its working only in Linux , not in AIX , any help??

Code:
cat abc.txt | sed -e 's/$a/$b/gI'  >  abc.txt


===============================

Example :- I have a variable value called "hello" in $a

Below the the file abc.txt.
Code:
HELLO , My name is mohit.

Now i need to search for $a i.e "hello" in abc.txt and replace it with another variable say $2 , whose value is "hi"

Sample O/P
Code:
hi , My name is mohit.


Last edited by Don Cragun; 09-04-2016 at 04:27 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 09-04-2016
This is VERY disturbing. The command:
Code:
cat abc.txt | sed -e 's/$a/$b/g'  >  abc.txt

will NOT do what you said it will do on any Linux system, on any UNIX system (including AIX), nor on any BSD system.

You haven't bothered to tell us what shell you're using, but with any of the common shells available on any of the systems listed above that I am aware of, the above command will do the same thing on every one of those systems: truncate abc.txt so that it will be an empty file.

If you weren't redirecting output to your input file (thereby destroying your input file before cat and sed ever see it), you are still using single quotes that cause $a and $b to be treated as literal strings instead of having the shell expand them.

If you want to run a script on an AIX system and on a Linux system and have it behave the same way on both of those systems, you cannot use options that are only available on one of those systems.

If you expect us to help you, you need to show us the actual results that you are getting on both systems. I.e., show us:
  • the contents of your file before you run your command(s) on your Linux system (in CODE tags),
  • the contents of the variables you are using in your commands on your Linux system (in CODE tags),
  • any diagnostic messages printed while running your command(s) on your Linux system (in CODE tags),
  • the contents of your file after you run your command(s) on your Linux system (in CODE tags),
  • the contents of your file before you run your command(s) on your AIX system (in CODE tags),
  • the contents of the variables you are using in your commands on your AIX system (in CODE tags),
  • any diagnostic messages printed while running your command(s) on your AIX system (in CODE tags), and
  • the contents of your file after you run your command(s) on your AIX system (in CODE tags),
and tell us:
  • what Linux distribution you're using (including version number),
  • what shell you're using on your Linux system (including version number),
  • what AIX version you're using, and
  • what shell you're using on your AIX system.
# 3  
Old 09-04-2016
Moderator's Comments:
Mod Comment This thread has been moved from a non-technical forum to a technical forum.
# 4  
Old 09-04-2016
Quote:
Originally Posted by Don Cragun
You haven't bothered to tell us what shell you're using
On AIX it is most probably a ksh88, which is the systems default shell. It is rarely changed to anything else.

For the rest of your objections: +1 from me!

bakunin
# 5  
Old 09-04-2016
Quote:
Originally Posted by bakunin
On AIX it is most probably a ksh88, which is the systems default shell. It is rarely changed to anything else.

For the rest of your objections: +1 from me!

bakunin
If the OP's original script had been developed on AIX, that would be my assumption as well. In this case, however, we seem to have a script that was developed on some unnamed Linux distribution (where bash or ash is much more common as the default shell). Therefore, I want the OP to explicitly state which shell is being used on both systems. (And, this is especially true if there is a shell on the Linux system that performs variable expansions in single-quoted strings!)
# 6  
Old 09-05-2016
Hi Guys ,
Thanks for all the details , and sorry for not being too descriptive about my problem.

Let me try to explain again with proper details.

I am writing a shell script in bash in which i want to search and replace a particular string at particular line using a variable in AIX, and i can accomplish that using below command.


For example :- I have a file with name abc.txt with below 2 lines
Code:
Hello , My name is MOHIT
I am learning shell script

Now ,i have some variables in my script that have following values
Code:
line_number=1
current_value=MOHIT
new_value=Ankit


code :
Code:
cat abc.txt | sed -e $line_number"s/$current_value/$new_value/g" > abc.txt


It works perfectly and gives me the O/P as
Code:
Hello , My name is Ankit
I am learning shell script


But i want to ignore case sensitiveness , again by example of a file , abc.txt , now i have mohit written in lower case
Code:
Hello , My name is mohit
I am learning shell script

Again , i have some variables in my script that have following values
Code:
line_number=1
current_value=MOHIT
new_value=Ankit

code :
Code:
cat abc.txt | sed -e $line_number"s/$current_value/$new_value/g" > abc.txt

This is not working , please suggest.

Note: I have tried using I at the end, its working for other linux distributions , but not on my AIX systems.

code:
Code:
cat abc.txt | sed -e $line_number"s/$current_value/$new_value/gI" > abc.txt

AIX :- OS version
7100-03-02-1412

Thanks in advance!
Moderator's Comments:
Mod Comment Please reread the forum rules you agreed to when you joined this forum.

Using CODE tags when displaying sample input, sample output, and code segments is required in all posts.

Last edited by Don Cragun; 09-05-2016 at 03:48 AM.. Reason: Add CODE and ICODE tags.
# 7  
Old 09-05-2016
As I said before, using any of the following commands:
Code:
cat abc.txt > abc.txt
cat abc.txt | sed "any sed arguments" > abc.txt
sed "any sed arguments" abc.txt > abc.txt

will result in abc.txt being a file of size 0, containing absolutely no characters. So, the code that you say is working on your Linux systems cannot possibly do what you say it is doing. The following should come close to what you said you want on a Linux distribution and on an AIX system:
Code:
#!/bin/bash
IAm=${0##*/}

file="abc.txt"
tmpfile="$IAm".$$

trap 'rm -rf "$tmpfile"' EXIT

line_number=1
current_value=MOHIT
new_value=Ankit

# Function to convert a string to a BRE that will match a case-insensitive
# version of that string.
# Usage: string2CIBRE "string"
string2CIBRE() {
	awk -v s="$1" '
		BEGIN {	for(i = 1; i <= length(s); i++)
			printf("%s", ((L = substr(s, i, 1)) ~ /[[:alpha:]]/) ? \
			    "[" tolower(L) toupper(L) "]" : L)
		}
	'
}

printf 'Contents of "%s" before sed:\n' "$file"
cat "$file"
sed "${line_number}s/$(string2CIBRE "$current_value")/$new_value/g" "$file" > "$tmpfile"  &&
	cp "$tmpfile" "$file"
printf '**********\nContents of "%s" after sed:\n' "$file"
cat "$file"

If abc.txt contains the following before you run the above script:
Code:
Hello , My name is Mohit, all lowercase: mohit, all uppercase: MOHIT
I am learning shell script
Hello , My name is Mohit, all lowercase: mohit, all uppercase: MOHIT
I am learning shell script

the output from the above script will be:
Code:
Contents of "abc.txt" before sed:
Hello , My name is Mohit, all lowercase: mohit, all uppercase: MOHIT
I am learning shell script
Hello , My name is Mohit, all lowercase: mohit, all uppercase: MOHIT
I am learning shell script
**********
Contents of "abc.txt" after sed:
Hello , My name is Ankit, all lowercase: Ankit, all uppercase: Ankit
I am learning shell script
Hello , My name is Mohit, all lowercase: mohit, all uppercase: MOHIT
I am learning shell script

and, as indicated by the output, the contents of abc.txt after running the script will be:
Code:
Hello , My name is Ankit, all lowercase: Ankit, all uppercase: Ankit
I am learning shell script
Hello , My name is Mohit, all lowercase: mohit, all uppercase: MOHIT
I am learning shell script

In addition to working with bash, this should also work with a 1988 or a 1993 version of the Korn shell (ksh) or any other shell that performs basic POSIX shell required parameter expansions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. 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

3. Shell Programming and Scripting

Search and replace in shell scripting

I am trying to write shell script to find and replace using Sed. but i am unable to complete the setting. need help in doing that. Requirement: FROM "${O_INSTANCE}/diag/logs/${C_TYPE}/${C_NAME}/httpd.pid" TO "/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid" (10 Replies)
Discussion started by: avmk0407
10 Replies

4. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

7. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question