-2

I have a line with string of characters (BCCDDDCDCCDDDDDDABCDABCABDBACBDCAACCBBCABACBCCABCACBCDCCCBDBACDCBBCBCBCCCACADAACCABABADBCBAABBBCCBB)

I'd like to replace a specific character (for e.g, 4th character) to lower case.

I have tried with this awk command;

awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }' input > output

input file contains the string

"BCCDDDCDCCDDDDDDABCDABCABDBACBDCAACCBBCABACBCCABCACBCDCCCBDBACDCBBCBCBCCCACADAACCABABADBCBAABBBCCBB"

This awk command gives this output:

"Bccdddcdccddddddabcdabcabdbacbdcaaccbbcabacbccabcacbcdcccbdbacdcbbcbcbcccacadaaccababadbcbaabbbccbb"

How do I change all the other characters to lowercase except the 4th character?

HatLess
  • 10,622
  • 5
  • 14
  • 32
  • please update the question with your expected output (for the given input); will the input have multiple lines and if so ... do you need to modify the 4th character in each line? could a line have more than one field (your example has just 1 field) and if so do you need to covert the 4th character of the 1st field or the 4th character of every field – markp-fuso Dec 28 '21 at 23:50
  • Does this answer your question? [Can I use awk to convert all the lower-case letters into upper-case?](https://stackoverflow.com/questions/14021899/can-i-use-awk-to-convert-all-the-lower-case-letters-into-upper-case) – Ron Dec 28 '21 at 23:56
  • Your question contains contradictory statements! First you say "_I like to replace a specific character (for e.g, 4th character) to lower case._" and then you end up saying "_Now, how do I change all the other characters to lowercase except the 4th character?_". So which is it? Please edit your question so it is clear and concise what it is you want and also add an example of the desired _output_ based on the shown _input_. – user3439894 Dec 29 '21 at 06:16

4 Answers4

1

On that line you can use something like this. Through -F '' every letter is now a field which can be accessed with $i.

$ cat line
BCCDDDCDCCDDDDDDABCDABCABDBACBDCAACCBBCABACBCCABCACBCDCCCBDBACDCBBCBCBCCCACADAACCABABADBCBAABBBCCBB

$ awk -F '' '{ for(i=1;i<=NF;++i){
    if(i!=4){
      printf("%s",tolower($i))}
    else{
      printf("%s",$i)} } print "" }' line
bccDddcdccddddddabcdabcabdbacbdcaaccbbcabacbccabcacbcdcccbdbacdcbbcbcbcccacadaaccababadbcbaabbbccbb
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
1

With GNU sed, would you please try:

sed -E 's/(.{3})(.)/\1\L\2/' YourFile

With python, assuming the variable s is assigned to the line,

print(s[0:3] + s[3:4].lower() + s[4:])
tshiono
  • 21,248
  • 2
  • 14
  • 22
0

An example in python of a function that makes it, I let you understand the logic and adapt it to your problem.

def f(s):
    # your result
    res = ""

    # iterate though each character in the string
    for i in range(0, len(s)):

        # on some condition, here 4th character, you add a lowercase character to your result
        if(i == 3): 
            res += s[i].lower()

        # on some other conditions an upper case character...
        elif condition_2_to_create: 
            res += s[i].upper()

        # or just the character as it is in the string without modification
        else:
            res += s[i]

    # Then return your result
    return res
0

Change 4th letter of every line to lowercase:

gawk '{$0 = substr($0,1,3) tolower(substr($0,4,1)) substr($0,5)} 1' YourFile

Or:

gawk -F '' '{$4=tolower($4)} 1' OFS='' YourFile

Or with Perl:

perl -pne 'substr($_,4,1) = lc(substr($_,4,1))' YourFile
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432