justintime justintime - 2 months ago 13
Perl Question

Awk/ Perl regular expression to match space with hyphen

I have the following 2 lines in sample.txt

AIA - 1000
AIA Integrations for E-Business Suite - 5544


Now i want to see the following output:

Column1 | Column2
AIA 1000
AIA Integrations for E-Business Suite 5544


i tried:

awk -F "-" sample.txt


It filters the hyphen "-" near "E-Business Suite"
How to make it filter the last hyphen instead of the intermediate ones.

Answer

You can use:

awk -F ' - ' -v OFS=';' 'BEGIN{print "Column1", "Column2"} {print $1, $2}' file |
column -s ';' -t

Column1                                Column2
AIA                                    1000
AIA Integrations for E-Business Suite  5544
  • -F ' - ' uses " - " is input field separator
  • -v OFS=';' uses ; as output field separator
  • column -s ';' -t formats data in tabular format using ; as delimiter
Comments