-1
 <testcase classname='tc1' name='tc1' time='78.455094'/>
  <testcase classname='tc2' name='tc2' time='78.549320'>
    <failure type='fail-verdict'>
      error message
    </failure>
  </testcase>
  <testcase classname='tc3' name='tc_3' time='78.444719'/>

how to replace 3-rd attribute time in testcase tag to attribute status (status="pass" or "failure" based on testcase; for example: tc1,tc3 - pass, tc2- fail). I tried sed: sed -i 's/time=\'[0-9]+\.[0-9]+'/status="pass"/g' file.txt firstly without any logic..but it doesn't work.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Volodymyr
  • 1,192
  • 21
  • 42

1 Answers1

1

You need the -E option (and -r for gnu sed) to use ERE (+ and more) or you should use [0-9][0-9]*

You cannot escape singlequotes inside single quotes, you need to do the following:

% echo 'hello'\'' world'
hello' world

Applied to your command:

sed -i 's/time='\''[0-9][0-9]*\.[0-9][0-9]*'\''/status="pass"/g' file.txt
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • thanks. It corectly changes the first part of my task. – Volodymyr Apr 20 '16 at 10:02
  • the one thing remains here: how can I modify the previous line of the file if in the next line I've found tag? Is it possible with sed or I should use grep or awk? – Volodymyr Apr 20 '16 at 10:06
  • @Wlodek If you are using gnu sed you can use the `-z` modifier which will slurp the whole file into "one line" and you can then match on `\n` for newlines. But as I said in regex is not suited to parse / modify xml – Andreas Louv Apr 20 '16 at 10:11
  • what do you recommend instead? – Volodymyr Apr 20 '16 at 10:14
  • @Wlodek You might want to take a look at: https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash – Andreas Louv Apr 20 '16 at 10:14