Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How to delete contents of a specific field, if it matches a pattern and there is nothing else in the field
Parent
How to delete contents of a specific field, if it matches a pattern and there is nothing else in the field
How do I delete contents of a specific field, if it matches a pattern, and there is nothing else in the field? I have a several GB tsv file, and I am interested in a specific field (72). If it contains hyphens, and only hyphens, then I want the hyphens deleted, leaving a blank field. I am using Ubuntu 20.04, with GNU awk v5. I've tried something like this:
awk 'BEGIN{FS=OFS="\t"}{gsub(/-/,"",$72)}1' file.tsv
But that also deletes the hyphens if there are other characters in the field too, which I do not want. E.g.
blah------
becomes
blah
but I want to leave it unchanged; but change
---------------
to nothing.
Post
You're matching the regex pattern of /-/
, so it just matches every individual hyphen, regardless of where. You want to match the entire entry if it's only hyphens, or /^-+$/
.
^
– Beginning of line
-+
– One or more hyphens
$
– End of line
Putting that in gives
awk 'BEGIN{FS=OFS="\t"}{gsub(/^-+$/,"",$72)}1' file.tsv
which does what you want.
0 comment threads