SED White Space replacement only between variables, regex usage

Viewed 63

i recently came across a tricky situation while doing log analysis, am not an expert in REGEX so just wanted to post here and see if i get any good solution

problem: Below i have pasted the result that i got by performing awk and grep command on a unix log file. using awk |sort |uniq -c

          `2851 ABC=10087 ACG=123 RMC=17`
          `2851 ABC=10087 ACG=123 RMC=17`
          `1323 ABC=10087 ACG=123 RMC=10`
          `1323 ABC=10087 ACG=123 RMC=20`
          `1323 ABC=10087 ACG=123 RMC=20`
           `248 ABC=10087 ACG=123 RMC=8`
           `248 ABC=10087 ACG=123 RMC=8`
           `236 ABC=10087 ACG=123 RMC=7`
           `127 ABC=10087 ACG=123 RMC=16`
           `127 ABC=10087 ACG=123 RMC=16`
            `67 ABC=10087 ACG=123 RMC=1`

I piped this above output ("|") and applied sed command to it to sort 7th feild (i.e RMC values in descending order), so i used this

sed's/=/ / g' |sort -rnk 7,7 .

After sorting the value i again changed the out to look like previous, so i re-applied the sed i used,

sed 's/=/ / g' |sort -rnk 7,7 |sed 's/ /=/g'

This is what i was expecting

       `1323 ABC=10087 ACG=123 RMC=20`
       `1323 ABC=10087 ACG=123 RMC=20`
       `2851 ABC=10087 ACG=123 RMC=17`
       `2851 ABC=10087 ACG=123 RMC=17`
        `127 ABC=10087 ACG=123 RMC=16`
        `127 ABC=10087 ACG=123 RMC=16`
       `1323 ABC=10087 ACG=123 RMC=10`
        `248 ABC=10087 ACG=123 RMC=8`
        `248 ABC=10087 ACG=123 RMC=8`
        `236 ABC=10087 ACG=123 RMC=7`
         `67 ABC=10087 ACG=123 RMC=1`

but my output looks something like this

    `===1323=ABC=10087=ACG=123=RMC=20`
    `===1323=ABC=10087=ACG=123=RMC=20`
    `===2851=ABC=10087=ACG=123=RMC=17`
    `===2851=ABC=10087=ACG=123=RMC=17`
    `====127=ABC=10087=ACG=123=RMC=16`
    `====127=ABC=10087=ACG=123=RMC=16`
    `===1323=ABC=10087=ACG=123=RMC=10`
    `====248=ABC=10087=ACG=123=RMC=8`
    `====248=ABC=10087=ACG=123=RMC=8`
    `====236=ABC=10087=ACG=123=RMC=7`
    `=====67=ABC=10087=ACG=123=RMC=1`

How can i achieve my desired output ?

5 Answers

You only need sort with -t option :

sort -t = -rnk 4

If you tell sort to take the equal sign as the field-seperator it works.

Why sed if you can do this?

echo "$response" | sort -t '=' -nr -k4
1323 ABC=10087 ACG=123 RMC=20
1323 ABC=10087 ACG=123 RMC=20
2851 ABC=10087 ACG=123 RMC=17
2851 ABC=10087 ACG=123 RMC=17
127 ABC=10087 ACG=123 RMC=16
127 ABC=10087 ACG=123 RMC=16
1323 ABC=10087 ACG=123 RMC=10
248 ABC=10087 ACG=123 RMC=8
248 ABC=10087 ACG=123 RMC=8
236 ABC=10087 ACG=123 RMC=7
67 ABC=10087 ACG=123 RMC=1

Regards!

I got rid of the backticks cause that was messing me up. You could try

$ echo "2851 ABC=10087 ACG=123 RMC=17
2851 ABC=10087 ACG=123 RMC=17
1323 ABC=10087 ACG=123 RMC=10
1323 ABC=10087 ACG=123 RMC=20
1323 ABC=10087 ACG=123 RMC=20
248 ABC=10087 ACG=123 RMC=8
248 ABC=10087 ACG=123 RMC=8
236 ABC=10087 ACG=123 RMC=7
127 ABC=10087 ACG=123 RMC=16
127 ABC=10087 ACG=123 RMC=16
67 ABC=10087 ACG=123 RMC=1"| sed 's/=/ / g' |sort -rnk 7,7| sed -e 's/ /=/2' -e 's/ /=/3' -e 's/ /=/4'

Which gives me

1323 ABC=10087 ACG=123 RMC=20
1323 ABC=10087 ACG=123 RMC=20
2851 ABC=10087 ACG=123 RMC=17
2851 ABC=10087 ACG=123 RMC=17
127 ABC=10087 ACG=123 RMC=16
127 ABC=10087 ACG=123 RMC=16
1323 ABC=10087 ACG=123 RMC=10
248 ABC=10087 ACG=123 RMC=8
248 ABC=10087 ACG=123 RMC=8
236 ABC=10087 ACG=123 RMC=7
67 ABC=10087 ACG=123 RMC=1

The problem is that your last sed change every blank space to =. Instead, try

sed 's/=/ / g' |sort -rnk 7,7 |sed 's/RMC /RMC=/g'

This might work for you (GNU sed):

sed 's/.*=\(.*\)/\1\t&/' file |
sort -nr |
sed 's/.*\t//'

Add the key(s) to be sorted to the start of the line, sort, remove the key(s).

In this case, however, the answers by Matias Barrios and Oliver Gaida are sufficient.

Related