Can field separator in awk encompass multiple characters?

后端 未结 5 1428
野的像风
野的像风 2020-12-31 07:05

Can I use a field separator consisting of multiple characters? Like I want to separate words which contain quotes and commas between them viz.

\"School\",\"College\"

5条回答
  •  生来不讨喜
    2020-12-31 07:52

    What's being talked around here is that the Field Separator isn't just limited to being multiple characters but can actually be a full-blown regex.

    To wit: This strips out the header and surrounding tags from an XML fragment. Note that tags are well-formed, but different.

    bash-3.2$ more xml_example 
    
    
    
    http://www.foo.com/about.html
    2006-05-15T13:43:37Z
    0.5000
    
    
    http://www.foo.com/articles/articles.html
    2006-06-20T23:03:36Z
    0.5000
    
    

    Now we apply the awk script to print out the middle field, using a regex as the field separator:

    bash-3.2$ awk -F"<(/?)[a-z]+>" '{print $2}' 

    The blank lines are from where a tag was the only thing on that line, so there is no $2 to print. This is actually really powerful because it means that you can not only use fixed patterns with multiple characters but the full power of regular expressions as well in your field separator.

提交回复
热议问题