2

How can I take a string like this:

sample="+TEST/TEST01/filetest01.txt"

And replace all occurrences of test01/TEST01 with test02/TEST02, keeping the text in the same case. So the desired output would be:

"+TEST/TEST02/filetest02.txt"

If you were to pass the replacement string of TEST03. Then the desired output would be

"+TEST/TEST03/filetest03.txt"

If the replacement text was Test04. The desired output:

"+TEST/TEST04/filetest04.txt"

I've tried this:

echo "$sample" | awk 'BEGIN{IGNORECASE=1}{gsub("test01", "test02");print}'

It replaces the lower case value but not the upper case.

I cannot use sed as the version I have doesn't support the /I switch to ignore case.

My end goal is to be able to use variables that represent the Item to change. So variables would be like this:

text2replace=test01
replacetext=test02
1
  • It's not clear if you want to replace test01 or TEST01 with test02 or if you need to replace test01 with test02 but TEST01 with TEST02 or something else. Also what if the input contained TeSt01 - should that become test02 or TEST02 or TeSt02 or something else? What if the original and replacement strings were different lengths? Add some concise, testable sample input and expected output covering all of the possible use cases to clarify your requirements,. Commented Aug 30, 2018 at 4:28

3 Answers 3

1

Try this using gnu-awk: gawk:

echo "$sample" | awk 'BEGIN{IGNORECASE=1}{print gensub("test01", "test02", "g")}'

Output

+TEST/test02/filetest02.txt

Last chance area

echo "$sample" |
    tr '[[:upper:]]' '[[:lower:]]' |
    awk '{gsub("test01", "test02");print}'
Sign up to request clarification or add additional context in comments.

5 Comments

It needs gawk version of awk
my only option at this point is awk I don't have Gawk on the OS I'm trying to do this on
Check my Last chance area part :)
Most version of tr don't require an extra [] around character classes, and you could just do '[:upper:]' '[:lower:]'; granted, the extra [] is a no-op as you replace it with itself.
I Need to retain the upper case on the first TEST because that is how the directory is setup... so that works partially
1

perl is good for this

$ perl -pe 's/test\K01/02/ig' <<< "+TEST/TEST01/filetest01.txt"
+TEST/TEST02/filetest02.txt

The \K directive instructs the regex engine to match what is on the left-hand side of it and then forget about it. It acts to position the "cursor" to the start of "01" only when it is preceded by "test".

I'm also using the i flag for case-insensitive matching.


More generally, if you looking to increment the digits following "test" case-insensitively (and zero-pad the same amount):

perl -pe 's/test\K(\d+)/ sprintf "%0*d", length($1), $1+1 /eig' <<INPUT
+TEST/TEST01234/filetest00009.txt
INPUT
+TEST/TEST01235/filetest00010.txt

7 Comments

the question has an awk tag and not a perl tag
I know. I don't let that constrain me: I try to present alternatives that I think are better.
I guess the purpose of the tag it to restrict one to the language asked. Otherwise confusion may arise.it is therefore necessary to answer in the language used. All languages can do this, python,c++,R, etc but OP specified awk. so we need to maintain that
I disagree. I consider the tag list as technologies that the asker thinks are relevent at the moment they are asking the question. You'll frequently see questions where the tags contain many irrelevant things. When answering with a "non-tag" technology, I often get comments like "oh, I never thought of using that".
On the other hand, if the question had said "I need to use awk because ..." then I would provide an awk answer.
|
1

You say you don't have GNU sed with its I flag, but you can do it with POSIX sed:

$ sed 's/\([Tt][Ee][Ss][Tt]0\)1/\12/g' <<< '+TEST/TEST01/filetest01.txt'
+TEST/TEST02/filetest02.txt

[Tt] is the poor man's case-insensitive match for T or t; the case is preserved by using a capture group.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.