5 votes
Accepted

Regular expression not matching the expected data

The issue is the regular expression. You would need to replace each * by .* to match any number of any character between the substrings. awk -v RS='' -v ORS='\n\n' '/func: peak.*down.*HIL/' file ...
  • 282k
4 votes
Accepted

awk: select lines begining with string and ending with string

Just use a single regex that matches both start and finish: awk '/^a.*g$/' input.txt Or, if you really want to use two, you can combine them with &&: awk '/^a/ && /g$/' input.txt
  • 221k
2 votes
Accepted

What is the function of this script passed to "sed -e"?

/^#/ !{ q } The part between the slashes is a regular expression, where ^ means the start of string and # is just the character itself. The pattern selects the lines on which run the ...
  • 121k
2 votes

How to pad with leading zeros without changing the last 0?

perl -pe 's/\d++(?!$)/sprintf"%03d",$&/ge' Would pad with zeros to a length of 3 the sequences of decimal digits that are not found at the end of the line. If the string is in a shell ...
2 votes

Perl-rename: regex for case-insensitive matching

While the /i modifier is the most common and well-known answer, it should also be said that this can be modified inside the regex itself. /(?i)b(\d{2}A)/ This can be useful for dynamically generated ...
  • 21
2 votes

awk: select lines begining with string and ending with string

No need for awk, just grep: grep "^a.*g$" input.txt
2 votes
Accepted

Delete multiline blocks with sed non-greedy

sed is an excellent tool for doing simple s/old/new/ operations. For anything else, just use awk for clarity, efficiency, robustness, portability, maintainability, etc. For example, using any POSIX ...
  • 24.7k
1 vote
Accepted

grep searching end of line pattern from DOS files

No, \r is not part of standard basic nor extended regular expressions except in awk, though some greps support it as an extension like the grep from ast-open which supports it in all its regexp ...
1 vote

add a new line after a specific string in a file?

I'm going to assume that echo is just a typo because unless there's something specific with your environment, that same command just gives the output sed '/Random/a\ newly added line' info.txt If you ...
  • 9,876
1 vote

Delete multiline blocks with sed non-greedy

Using GNU sed $ sed -Ez 's~ +/?\*+ START( \*)?([^*]*\*+)([^\n]*\n[^*]*\*+)? END[^\n]*\n~~g' input_file class MyClass { keepField; construct() { this.keepField = 'keep'; } } ...
  • 2,187
1 vote

awk: select lines begining with string and ending with string

To make the answer as generic as possible using awk, here is an alternate way to perform the desired action, where pattern string is passed as variable from the command line. Demonstration test data ...

Only top scored, non community-wiki answers of a minimum length are eligible