8

We use the awk utility to employ flip flop logic to toggle from on state to off as shown: $ awk -v str="==" ' $0""==str{f=!f;next};f ' testfile Last test Using Posixly sed constructs we can implement the flip flop logic: sed -ne ' /^==$/{ x; # access state info from hold s/^$/0/; # initialize state y/01/10/; #...


3

LC_ALL=C find . -name '*.log' ! -name '*.log.*' To find the files whose name ends in .log but otherwise does not contain .log. As a zsh -o extendedglob glob: print -rC1 - (^*.log.*).log or (as a closer equivalent to the find one above): print -rC1 - *.log~*.log.* Or as a ksh or zsh -o kshglob or bash -O extglob glob: printf '%s\n' !(*.log.*).log (set the ...


3

The ~n tells apt that you're trying to match the name as a regular-expression as opposed to a simple string comparison. A regular-expression of nvidia means match anything that has the substring nvidia within it, as opposed to matching the complete string nvidia. Therefore you get a far longer list. More details here. Note that this feature is only ...


3

I'd use perl: $ perl -0777 -ne 'print $3 while /^((\S)\2+\n)(.*?)^\1/smg' < your-file This is a test Another test Last test Or pcregrep: $ pcregrep -Mo3 '(?s)^((\S)\2+\n)(.*?)\n?^\1' < your-file This is a test Another test Last test If it's only about returning what's between fixed delimiters: $ pcregrep -Mo1 '(?s)^==\n(.*?)\n?^==$' < your-file ...


3

tl;dr $ sed '/^==*$/,//{//!p};d' testfile This is a test Another test Last test On first look, a simple range could print all pairs (no loops needed): $ sed -n '/^=/,//p' testfile === This is a test === ==== Another test ==== == Last test == That prints every line between a line that starts with = and the next repeated regex (\\). That could be improved to ...


3

This awk command is quite readable: awk ' BEGIN {FS = "[,\t]"; OFS = "\t"} {for (i=3; i<=NF; i++) print $1, $2, $i} ' file In perl, this is perl -F'[,\t]' -lane 'print join "\t", @F[0,1], $F[$_] for 2..$#F' file # or perl -F'[,\t]' -slane 'print @F[0,1], $F[$_] for 2..$#F' -- -,=$'\t' file If you're not sure you have ...


3

Use the -a switch to get each line split into the @F array on whitespace. perl -lane 'print join "\t", @F[0, 1], $_ for split /,/, $F[2]'


3

You could do things with the hold buffer in sed, but it's easier to work around that with -z in GNU sed to tell it to use the NUL byte as a separator instead of a newline: $ sed -z 's/abc\ndef//' test.txt hij klmn or just switch to Perl (-0 would use the NUL as line separator, -0777 tells it read the whole file in one go): $ perl -0777 -pe 's/abc\ndef//' ...


3

Seems like your question wasn't clear and you got various types of solution. To get only content surrounded by == with GNU sed (syntax/feature might vary with other implementations): $ sed -n '/^==$/,//{//!p}' testfile Last test sed has a way to select a range of lines by specifying two addresses separated by a comma. To avoid repeating the regexp you can ...


2

The .* after folder\/ matches greedily, leaving only the final / to match in your second capture group. Instead, you can use [^/]* to match everything up to the next separator: sed 's/\(folder\/[^/]*\)\(\/.*\)/\1/' or (since you don't actually use the second group) sed 's/\(folder\/[^/]*\)\/.*/\1/'


2

IIUC, you want to print paths only up to the depth of subfolder. In that case you can use cut: your_command | cut -d/ -f1-2 or awk: your_command | awk -v FS='/' '{print $1 "/" $2}'


2

You can use the following regex: .*@island\.ac\.kr$ Example: $ cat /tmp/111 Tom.riddle@island.ac.kr Ron_Weasley@island.ac.kr harry2020@island.ac.kr itai@gmail.com what.ever@island.ac.kr abc@what.ac.kr $ grep '.*@island\.ac\.kr$' /tmp/111 Tom.riddle@island.ac.kr Ron_Weasley@island.ac.kr harry2020@island.ac.kr what.ever@island.ac.kr The dots after the @ ...


2

With perl: perl -0777 -pi -e ' s{^info\s*\{.*?\K(?=\})}{join "", map {"work day$_\n"} 4,5,8}mse; s{^work\s*\{.*?\K(?=\})}{Absent No\n}ms' /home/count/1/details/info.txt


2

awk 'BEGIN {RS=ORS="\n\n"}; /^info {/ { gsub("}","work day4\nwork day5\nwork day8\n}") }; /^work {/ { gsub("}","Absent No\n}") }; 1' info.txt This tells awk to read the input file a paragraph at a time (i.e. records are separated by two newlines, not just one, by setting the input record ...


2

I'd use awk for this with a state machine. I use the flags blank and block to indicate a blank line and a block awk ' /^$/ { blank++ } # Blank line blank && /^>/ { blank=0; block++; print "<blockquote>" } # First ">" line after blank block && blank { ...


2

No, there is no guarantee as to what exactly [a-z] will match, period. Well, in any other locale than "C" (when the utility comply with POSIX ). The core issue is with the "range" expression (using -). An explicit list like [abcdefghijklmnopqrstuvwxyz] will never fail. POSIX requests that a-z is exactly abcdefghijklmnopqrstvwxyz, yes, ...


2

Your sed command, as it stands in the question, would not be able to see any newline characters in its input at all since sed is a line-oriented editor. The data will be presented to sed one line at a time, with the actual newline characters removed. Adding -z with GNU sed would make the utility use nul (\0) as its delimiter. This means you now would get ...


2

Even though it's HTML and not proper XML you can actually do this with xmlstarlet. Let's call your file index.html. Command invocation: xmlstarlet fo -H index.html 2>/dev/null | xmlstarlet sel -t -v '//a[@title="view quote" and string-length(text()) > 1]' -n 2>/dev/null Output: Everything in life is luck. The first thing the secretary ...


2

You can do the following with GNU sed: $ sed 's:\*\*\([^*]*\)\*\*:<b>\1</b>:g' a.txt I like reading <b>books</b> and taking <b>notes</b> The \*\*\([^*]*\)\*\* pattern captures the string that is between **...** that is not a wildcard *. Finally you replace the captured group for itself between the tags.


2

Use lsb_release -s -r or lsb_release --short --release This gives you the release number without the Release: heading.


2

command: awk '{a[++i]=$0}/==/{for(x=NR-1;x<NR;x++)print a[x]}' filename|sed '/^$/d' output This is a test Another test Last test


2

The code in the question makes no sense as I don't know what language it is written in. The regular expression ^[0-9]+.+[0-9]$ is a POSIX extended regular expression that matches one or more digits at the start of the line, followed by one or more other characters (possibly including digits), and a final digit at the end of the line. The components of the ...


1

The following match should do what you want: syn match noneIndentError /^\( \{4\}\)*\zs \{,3\}/ Using \zs, you can exclude the pattern before it from the match, so it's a simpler way to write zero-width look-arounds (it's similar to \K in Perl). Then it matches up to 3 spaces. Here's how it matches various lines: foo # bar baz ...


1

I don't know if this is possible with fzf. But it does have a couple of special characters like ^ (must start with), $ (must end with), ! (must not contain). As for searching within files, you can do this with either the silver searcher (ag) or rigrep (rg) assuming they're installed by using the commands :Ag or :Rg You can also take advantage of adding ...


1

GNU sed in extended regex mode -E we use the range operator , to select the right block and within that block use the regex of the s/regex/repl/ to zero in on the right line to make the edits. sed -Ee ' /\[ req_distinguished_name ]/,/^$/s/^(\s*commonName\s*=).*/\1__NEWVALUE__/ ' file


1

With zsh instead of bash, you can do something approaching by replacing [BCDE] with {BCDE}: $ set -o braceccl $ print -l A{BCDE}GT ABGT ACGT ADGT AEGT If your input is guaranteed to contain only word characters and [, ] characters: set -o braceccl input='A[BCDE]GT' eval "output=( ${${${input//[^][[:IDENT:]]}//\[/{}//\]/\}} )" print -l $output


1

Your range [\d-_] does not make sense (there is no range starting from \d and running until _). Did you mean [\d_-]? The POSIX equivalent of [\d_-] is [[:digit:]_-] or [0-9_-]. Note that to include a literal dash in the bracket expression, it has to occur first or last. $ grep -o '[[:digit:]_-]\{1,\}' file 2021-04-21_08-45_ 2021-04-21_08-15_ $ sed -n 's/.* ...


1

Sed works on "records" (lines) which are defined by the presence of a trailing newline (\n) character. This means you cannot match past a \n because as far as sed is concerned, the \n is the end of the record. You can get around this, in GNU sed, by using -z to slurp the file and treat the entire thing as a single record (unless your file has NULLs ...


1

Another option here is the nest --explode action of Miller mlr --nidx --fs tab nest --explode --values --across-records --nested-fs ',' -f 3 file or using the shorthand nest specifier mlr --nidx --fs tab nest --evar ',' -f 3 file


1

You can also make it the following: '^comment[\s*:]*(\S+| +)$' Changed .* to \S+ so that we are either matching any non whitespace OR the spaces (explained further below) followed by end of line or spaces followed by end of line. The | +) is to also match any number of spaces (one or more) within the set. The set is then expected to follow with an end of ...


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