Find number of characters in a column and replace

Hi all,

I want to count total no. of characters in a column. and if no. of charaters are more than 3 then it must replace it by splitted string. ie, it must place a space after 3 characters.

Ex:

21 435g asd3dd jklfjwe wer

column number 3 has 4 alphanumeric character, so it must be splitted like:

21 435g asd 3dd jklfjwe wer

Thanks in advance.:slight_smile:

What have you tried so far?

I am still in learning phase of linux.....
I have tried "length" command to count the characters of a string.
then I tried to use "cut" and "fold" commands but cant corelate them.
This is not my assignment task.....I need to learn this for other purpose. So, kindly help me out in this.

I noticed some inconsistencies in your requirement!

If you want to split each alpha-numeric string greater than 3 character length, then why the following highlighted 4 character length string is not considered?

21 435g asd 3dd jklfjwe wer

By the way if your requirement is to split strings that are greater than 4 character length, then try this code:

awk '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( length ($i) > 4 )
                        {
                                if ( $i ~ /[a-zA-Z]/ && $i ~ /[0-9]/ )
                                {
                                        gsub( /.../, "& ", $i )
                                        sub(" $", X, $i )
                                }
                        }
                }
        }
1 ' file
1 Like