skip to Main Content

I am trying to run the following command of sed :

echo "fOO:paSS,tesT2:fail,TESt:pasS,fdfdhfd:pass,test5:anyresult test,test6:pass asfas " | sed  's/:.*/L&/;s/w+/u&/g;s/:/ # /g;y/,/n/' | sed 's/w+/&n/2;P;d'

The output which I am getting is :

FOO # Pass
TesT2 # Fail
Test # Pass
Fdfdhfd # Pass
Test5 # Anyresult
Test6 # Pass

Desired Output :

fOO # Pass
tesT2 # Fail
TESt # Pass
fdfdhfd # Pass
test5 # anyresult
test6 # Pass

What I want is :

  1. "Pass" & "fail" should always have "P" & "F" capital, no matter whatever input is given.
  2. Name of the "Test" should remain same as provided in echo command , it should not get changed.
  3. After and before of # there should be only first word present. If user gives input like "Test5:Pass successfully" then successfully should not get printed.

Optional requirement:

  • Can we convert this to a table anyhow of 2 columns and ‘n’ rows?

2

Answers


  1. I can’t help but think this must be a lot easier with Awk.

    awk BEGIN { RS=','; FS=':' }
    NF > 2 { print $1 " # " (tolower($2)s~ /^(test/fail)$/ ? toupper(substr($2, 1, 1)) tolower(substr($2, 2)) : $2) }'
    
    Login or Signup to reply.
    1. Using sed, tr, and cut to match the described spec of what the
      output should be, (rather than the inconsistent desired output in
      the OP):

      echo "fOO:paSS,tesT2:fail,TESt:pasS,fdfdhfd:pass,test5:anyresult test,test6:pass asfas " | 
         tr ',' 'n' | 
         sed 's/:[fp]/U&/;s/:/ # /;s/...$/L&/' | 
         cut -d ' ' -f '1-3'
      

      Output:

        fOO # Pass
        tesT2 # Fail
        TESt # Pass
        fdfdhfd # Pass
        test5 # anyresult
        test6 # Pass
      
    2. Two column variant output:

      echo "fOO:paSS,tesT2:fail,TESt:pasS,fdfdhfd:pass,test5:anyresult test,test6:pass asfas " |
         tr ',' 'n' | 
         sed 's/:[fp]/U&/;s/:/t/;s/...$/L&/' | 
         cut -d ' ' -f 1
      

      Output:

      fOO Pass
      tesT2   Fail
      TESt    Pass
      fdfdhfd Pass
      test5   anyresult
      test6   Pass
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search