shell script help

Hello everyone

i have a text file that contains something like this.

1 AC AT RR cOO
2 EE cFF HGB cEVY
3 WDU RWS cTY NBE

but it contains a lot of rows and that's an example of the data

what i want to do is to to create another text file using a shell script from this text file with these modifications:
1-let the same ordering of the same file
2-change each value that contains c to without c
3-add c to each value that does not contain c

e.g(1 AC AT RR cOO ->will become-> 1 cAC cAT cRR OO)

thanks in advance

awk '
    {
        for(x=1; x<=NF; x++){
            if($x !~ /c/ && x>1){
                $x="c" $x
            }
            else{
                sub(/c/,"",$x)
            }
        }
        print
    }
' infile

Output:

1 cAC cAT cRR OO
2 cEE FF cHGB EVY
3 cWDU cRWS TY cNBE
1 Like
sed 's/ / c/g;s/cc//g' yourfile

This code works according to the given example, but if you have something like

56 cEFcH OccK LI

it will return

56 EFcH cOK cLI

Note that :

  1. only the leading c will be deleted from the cEFcH occurence
  2. the OccK will be changed in OK (so as a prerequisit, you must ensure you don't have "cc" occurence appearing inside the values of the field you are parsing)

This can be enforced a little adding the leading space :

sed 's/ / c/g;s/ cc/ /g' yourfile

But the code was still so simple, i couldn't resist to post it

# cat tst
1 AC AT RR cOO
2 EE cFF HGB cEVY
3 WDU RWS cTY NBE
# sed 's/ / c/g;s/ cc/ /g' tst
1 cAC cAT cRR OO
2 cEE FF cHGB EVY
3 cWDU cRWS TY cNBE
1 Like

thanks a lot , both of the methods works :slight_smile:

You are right, ctsgnb. Changing the sub versus a gsub will fix that issue too.