2

I'd like to do a wholesale reformatting of our tests, and I'm cleaning up some inconsistent capitalization. I'm thinking of using awk to do this, since sed falls a little short, and since I need lookahead for my case. Specifically, for each line in a given file, I want the following to happen:

  • Look for the word it, describe, or context, followed by a space, followed by a single or double quote mark, followed by an uppercase alphabetic character.

  • If there's a match, substitute the match with the lowercased version of the entire matched string, but only the matched string (don't lowercase other things on the same line).

  • Don't match if the string after the single or double quote mark is one of "GET", "POST", "PUT", or "DELETE".

So, for example:

describe 'apple banana'       ----> (no change)
describe 'APPLE BANANA'       ----> describe 'aPPLE BANANA'
describe 'Apple Banana'       ----> describe 'apple Banana'
describe "Apple Banana"       ----> describe "apple Banana"
describe 'one TWO'            ----> (no change)

context 'POST BANANA'         ----> (no change)
context 'XPOST BANANA'        ----> context 'xPOST BANANA'

What awk arguments and/or other commands should I use? (It's okay with me if it requires more than one pass to do it.)

3
  • Will it, describe, or context always be the first word on the line? Will there always be exactly two words between the quotes? Oct 1, 2012 at 19:54
  • @Scott (1) it, describe, and context will always be the first word, but they may not start that line. (2) There might be many words between the quotes. Oct 2, 2012 at 2:21
  • @JohnFeminella Do you want me to update my answer so that it takes these scenarios into account as well? Thanks
    – p_strand
    Oct 2, 2012 at 17:57

1 Answer 1

3

DISCLAIMER:

This solution will strip all "extra" whitespaces on the lines that are replaced. For example...:

      describe           'Apple           Banana'

...will be replaced with:

 describe 'apple Banana'

However, the "extra" whitespaces in...:

            context      "GET  BANANA"

...will not be removed.


Here's an example in awk as requested (please note that you can execute the command on one line. The line breaks are only for visual appeal here on Super User):

cat someTextFile.txt | awk '{ \
    if( \
        ($1=="describe" || $1=="it" || $1=="context") \
         && (substr($2,0,1)=="\"" || substr($2,0,1)=="'"'"'") \
         && !(substr($2,2,length($2)-1)=="POST" \
              || substr($2,2,length($2)-1)=="GET" \
              || substr($2,2,length($2)-1)=="PUT" \
              || substr($2,2,length($2)-1)=="DELETE") \
       ){ \
          subStr=substr($2,0,1); \
          subStr2=tolower(substr($2,2,1)); \
          restStr=substr($2,3,length($2)-1); \
          print $1" "subStr""subStr2""restStr" "$3 \
        }else{ \
          print \
        } \
     }' 

OUTPUT:

 describe 'apple banana'
 describe 'aPPLE BANANA'
 describe 'apple Banana'
 describe "apple Banana"
 describe 'one TWO'

 context 'POST BANANA'
 context 'xPOST BANANA'

EDIT: here's the command without the line breaks cat someTextFile.txt | awk '{ if( ($1=="describe" || $1=="it" || $1=="context") && (substr($2,0,1)=="\"" || substr($2,0,1)=="'"'"'") && !(substr($2,2,length($2)-1)=="POST" || substr($2,2,length($2)-1)=="GET" || substr($2,2,length($2)-1)=="PUT" || substr($2,2,length($2)-1)=="DELETE") ){ subStr=substr($2,0,1); subStr2=tolower(substr($2,2,1)); restStr=substr($2,3,length($2)-1); print $1" "subStr""subStr2""restStr" "$3}else{print}}'

If you wish to write the output to a new file, just add > output.txt at the end of the command.

If you wish to execute this on multiple files and store the result in one textfile, simply swap out cat someFile.txt with a cat command that executes on the files that you want to be formatted e.g. with cat *log* or cat $(find /some/path -name "*log*")

EDIT EDIT: Thanks a lot Scott for the feedback!

14
  • Goodness! I wound up doing this in a Ruby oneliner instead of awk, but this solution does work. High-five! Oct 2, 2012 at 2:23
  • Will you please post the Ruby oneliner as an alternative solution? Oct 2, 2012 at 2:40
  • The multi-line version needs \ characters at the ends of the lines that are interior to the if statement. Oct 2, 2012 at 15:59
  • 1
    @p_strand: Regarding *: When you say cat *log* — in Unix/Linux, at least, and probably in any implementation of bash — the cat program doesn’t see the “*log*” argument. It sees (in my second example) three arguments: “logical”, “my_blog”, and “test.log”, because the shell (bash, the C shell, or whatever you’re using) does the expansion. In your example: do you have any file(s) matching *con* in the current directory when you run that find command? Oct 2, 2012 at 18:41
  • 1
    @p_strand: Regarding editing an accepted answer, I’m not sure of the etiquette myself. I’d say it’s OK as long as you broaden your answer; i.e., change it to something as good as what you posted originally, but that covers more cases. Fixing it so it can handle more than two words between quotes is an example of that. Adding a disclaimer that your solution strips white space is certainly valid, and I would encourage you to add the backslashes. Oct 2, 2012 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .