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
# 8  
Old 09-05-2016
Don Cragun is - as always - right on the money: what you showed us will not work at all (at least not on any Linux- or UNIX-system that i know of) in the presented way.

Here is another take, based on sed in a subfunction. Note that it will have some limitations regarding escaped strings because the shell may eat away some of the escapes during parsing (but, honestly, i found the parsing of the input string solely with shell expansion to be neat - admittedly, it is a showoff):

Code:
#! /usr/bin/ksh

function pReplaceCaseInsensitive
{
typeset     chInRegex="$1"
typeset     chReplacement="$2"
typeset     fIn="$3"
typeset     chOutRegex=""
typeset     chBuf=""
typeset -u chUpChar=""
typeset -l chLowChar=""
typeset    chInline=""

while [ -n "${chInRegex}" ] ; do
     chBuf="${chInRegex##?}"                  # chop off first character
     chLowChar="${chInRegex??${chBuf}}"       # get first char, invert to lower
     if print - "$chLowChar" | grep -q '[abcdefghijklmnopqrstuvwxyz]' ; then
          chUpChar="$chLowChar"
          chOutRegex="${chOutRegex}[${chLowChar}${chUpChar}]"
     else
          chOutRegex="${chOutRegex}${chInRegex%%${chBuf}}"
     fi
     chInRegex="$chBuf"
done

if [ -n "$fIn" ] ; then                       # deal with input from a file if specified
     if [ -r "$fIn" ] ; then
          sed 's/'"$chOutRegex"'/'"$chReplacement"'/g' "$fIn"
     else
          print -u2 "file $fIn is not readable or does not exist."
          return 1
     fi
else                                        # deal with input from stdin otherwise
     while read chInLine ; do
          print - "$chInLine" | sed 's/'"$chOutRegex"'/'"$chReplacement"'/g'
     done
fi

return 0
}

# main()

# first variant: use in a pipeline:
print - "Willy willy WIlLy test" | pReplaceCaseInsensitive 'willy' "Text"
# produces: "Text Text Text test"
print - "Willy willy WIlLy test" | pReplaceCaseInsensitive 'willy *' "Text"
# produces: "TextTextTexttest" (space gets eaten by the regexp " *")

# second variant: use with a file
pReplaceCaseInsensitive 'willy' "TEST" myfile

exit 0

Contents of myfile:
Code:
# cat myfile
Willy
willY
WiLlY
bla

Results of the second call to pReplaceCaseInsensitive():
Code:
TEST
TEST
TEST
bla

I hope this helps.

bakunin

Last edited by bakunin; 09-05-2016 at 06:32 AM..
# 9  
Old 09-07-2016
Thanks for the response. I will try this thing.

I just have one more issue related to find and replace with sed.

Lets say i have a file abc.txt like below
Code:
nas1:/var/core/test        /test
nas1:/var/core/test/test2  /test1

Now i just need to search and replace only "nas1:/var/core/test" which i have in form of variable. It should not touch "nas1:/var/core/test/test2".

Any help on this , thanks in advance !


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-07-2016 at 07:16 PM.. Reason: Added/changed CODE tags.
# 10  
Old 09-07-2016
Do you have a GNU sed available?
Code:
sed 's#nas1:/var/core/test #\U&#' file
NAS1:/VAR/CORE/TEST        /test
nas1:/var/core/test/test2  /test1

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