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.

Post History

80%
+6 −0
Q&A How to delete contents of a specific field, if it matches a pattern and there is nothing else in the field

The awk gsub function takes as its first argument a regular expression indicating the substring to be replaced, and replaces a matching substring with the value of the second argument, which is the...

posted 2y ago by Canina‭

Answer
#1: Initial revision by user avatar Canina‭ · 2022-04-08T18:06:53Z (over 2 years ago)
[The awk `gsub` function](https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#index-gsub_0028_0029-function-1) takes as its first argument a regular expression indicating the substring to be replaced, and replaces a matching substring with the value of the second argument, which is the replacement string.

Since your regular expression, `/-/`, is not anchored, it will match *anywhere* in the value that is being matched against. That's why it matches the hyphens that are contained as a part of the value. Here's an example run to illustrate this:

    $ (printf '%s\n' 'blah---blah' '---blah---' 'blah------' '---------------') \
    > | awk '{gsub(/-/,"",$1)}1'
    blahblah
    blah
    blah
    
    $

(note the empty line)

To get the behavior you are after, you need to *anchor* the regular expression at both the beginning and the end of the string, and provide a regular expression that matches against that whole string. Since you want to do a replacement only of strings that consist solely of hyphens, **the regular expression should instead be `/^-+$/`** where the `^` anchors at the beginning, the `-+` specifies an unbounded but non-zero number of `-` characters (that's the `+`), and the `$` anchors at the end.

Example:

    $ (printf '%s\n' 'blah---blah' '---blah---' 'blah------' '---------------') \
    > | awk '{gsub(/^-+$/,"",$1)}1'
    blah---blah
    ---blah---
    blah------
    
    $

As you can see, the contents of a field that consists of something other than only hyphens is now preserved, but the all-hyphens field contents is replaced by the empty string.