In gawk, is it correct that match(), gensub(), gsub() and sub() don't consider the fields, and are not affected by change to FS and OFS? In other words, are they only concerned about records not fields? Thanks.


Those functions all work on a specific string that you pass to the function, or, in the case of gensub(), gsub(), and sub(), on $0 (i.e. the complete input record) if you don't supply a string. They will not split the given string into fields or use either of FS or OFS.

So, yes, none of these functions are affected by a change of FS or OFS. They are considering the given string as it is. If $0 is used as the input string, this whole string is used unaltered.

This does not stop you from using e.g. gsub() on a separate field, obviously.

  • Thanks. How about match()? – Tim Nov 13 '18 at 22:32
  • @Tim match() requires that you explicitly give it exactly the string that you want to match against as the first argument. It does not default to matching against $0. – Kusalananda Nov 13 '18 at 22:34
  • I guess you mean match() is not concerned with fields. – Tim Nov 13 '18 at 22:35
  • Unrelated: may I ask if this reply is wrong? unix.stackexchange.com/a/481530/674 – Tim Nov 13 '18 at 22:35
  • @Tim None of them are. They are only concerned with strings. – Kusalananda Nov 13 '18 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.