Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

+8
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+6
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

While using `gsub()` won't hurt, aside from maybe a small performance hit and making readers wonder w... (1 comment)
While using `gsub()` won't hurt, aside from maybe a small performance hit and making readers wonder w...
Ed Morton‭ wrote almost 2 years ago

While using gsub() won't hurt, aside from maybe a small performance hit and making readers wonder what use case you're considering by using it, you should be using sub() for this as you're only doing 1 substitution (of the entire field at once).