Home > Software design >  How to use AWK to recognize sections through whitespace indentations
How to use AWK to recognize sections through whitespace indentations

Time:05-01

Here is the data file:

foo:
   item1: 1
   item2=2

bar:
   item1 = 100
   item2 : 200

The whitespace indent is used to designate sections, e.g. "foo" and "bar" are section start because they are at the start of the lines. But AWK by default removes whitespace, this makes me wondering how to write the matching pattern.

I tried this:

$0 ~ ^[a-z] :  {print "section title";}
$0 ~ ^[ ] [a-z]   {print "item";}

But I got syntax errors:

awk: /tmp/2.awk:1: $0 ~ ^[a-z] :  {print "section title";}
awk: /tmp/2.awk:1:      ^ syntax error
awk: /tmp/2.awk:1: $0 ~ ^[a-z] :  {print "section title";}
awk: /tmp/2.awk:1:           ^ syntax error
awk: /tmp/2.awk:2: $0 ~ ^[ ] [a-z]   {print "item";}
awk: /tmp/2.awk:2:      ^ syntax error
awk: /tmp/2.awk:2: $0 ~ ^[ ] [a-z]   {print "item";}
awk: /tmp/2.awk:2:               ^ syntax error

Please help.

[UPDATE] Thanks RavinderSingh13, now it works:

>cat /tmp/2.awk
$0 ~ /^[a-z] :/  {print "section title";}
$0 ~ /^[ ] [a-z] /  {print "item";}

>awk -f /tmp/2.awk  /tmp/data
section title
item
item
section title
item
item

CodePudding user response:

Another awk idea:

$ awk 'NF {print ($0 ~ /^[[:space:]] /) ? "item" : "section title"}' data
section title
item
item
section title
item
item

CodePudding user response:

Regarding AWK by default removes whitespace - no, it doesn't. Not sure what you're thinking of with that.

In your code you just forgot to put regexp delimiters /.../ around your regexp so you COULD write it as:

$0 ~ /^[a-z] :/  {print "section title";}
$0 ~ /^[ ] [a-z] /  {print "item";}

but:

  1. you don't need to put a blank inside a bracket expression ([ ]) as it's already literal
  2. You don't need to write $0 ~ /foo/ as just /foo/ means the same thing
  3. You don't need to test for [a-z] as that's all there is after any leading blanks

so all you actually need is:

$ awk 'NF{print (/^ / ? "item" : "section title")}' file
section title
item
item
section title
item
item
  • Related