AWK Field Separators Vanishing

邮差的信 提交于 2020-01-30 06:07:40

问题


I am trying to use AWK to modify a long SQL script after removing a column from the database.

The lines I am trying to modify:

INSERT INTO `table` (`one`, `two`, `three`, `four`, `five`, `six`) VALUES ('alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot');

My AWK script:

BEGIN { 
    FS=","
} 
/`table`/ { $3=$8=""; print }

Which I run with:

awk -f script.awk in.sql > out.sql

This does ALMOST what I want it to do, except it seems to remove all of the Field Separators(commas):

INSERT INTO `table` (`one`  `two`   `four`  `five`  `six`) VALUES ('alpha'  'bravo'   'delta'  'echo'  'foxtrot');

What I would have expected to see is a double comma, that I would need to replace with a single comma using gsub or something.

What is happening to the commas?


回答1:


You can set the output field separator, OFS, to a comma, to better preserve the input syntax.




回答2:


When you assign a value to a field, awk reconstructs the input record using the Output Field Separator, OFS. You have 2 choices:

  1. Set FS=OFS="," instead of just FS="," so the record gets rebuilt using commas, or
  2. Do not assign a value to a field, but instead use REs to operate on the whole record.

You already know how to do "1" and that you'd then need to strip double OFSs to remove empty fields, here's how to do "2" using GNU awk for gensub():

$ awk '{print gensub(/(([^,]+,){2})[^,]+,(([^,]+,){4})[^,]+,/,"\\1\\3","")}' file
INSERT INTO `table` (`one`, `two`, `four`, `five`, `six`) VALUES ('alpha', 'bravo', 'delta', 'echo', 'foxtrot');

Note that since you aren't doing anything with individual fields, you don't need to specify FS or OFS and the record doesn't get recompiled so you don't need to remove resultant empty fields which can often be error-prone

You can do the same thing with sed if you prefer.



来源:https://stackoverflow.com/questions/19183523/awk-field-separators-vanishing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!