New answers tagged

3 votes
Accepted

How to leave only the first word in each line with sed, cut or awk?

Assuming a space separates your words, you don't need to use a regular expression to extract only the first space-delimited word. Instead, use cut with space as the delimiter: cut -d ' ' -f 1 file ...
  • 311k
0 votes

How should I replace a number in a JSON file with its MD5 hash using sed command?

The following command uses Miller (mlr) to parse the JSON input (Miller release 6 or later should use --jsonl in place of --json to read "JSON lines"). It then uses the put sub-command to ...
  • 311k
0 votes

How should I replace a number in a JSON file with its MD5 hash using sed command?

Solving a similar problem, I found this question. The offered solution didn't quite work for me because I wanted to solve it without a loop. Below is a proof of concept how to solve the problem ...
  • 1
1 vote

Extract lines from indented output

Using any awk (and cat file inplace of sudo ddcutil capabilities to demonstrate the answer): $ cat file | awk '$1=="Feature:"{f=($2 == "60"); next} $1=="Values:"{next} ...
  • 27.1k
0 votes

How to show only interfaces starting with a certain pattern

For post-processing, with newer versions of iproute2 you may want to use the JSON output format that is meant for that: ip -j -s link show | jq -r ' .[]| select(.ifname|test("^s\\d+-eth\...
-2 votes

How to show only interfaces starting with a certain pattern

Previous answers suggested: ifconfig: which is deprecated. ip -s link: which doesn't display all the information that ifconfig displays (such as IP address). I wanted to offer another command that ...
4 votes
Accepted

sed's greedy match shouldn't match this string, but does

A greedy matching system just means it will try to find the largest matching string (meaning the first largest, it will stop searching at the first match for the whole regex), not that it will stop at ...
  • 228k
0 votes

Extract lines from indented output

Using sed $ sudo ddcutil capabilities | sed -En '/Feature: 60/{n;/Values:/{:a;n;/Feature:/,/Feature:/!s/^[ \t]+//gp;ba}}' 11: HDMI-1 12: HDMI-2 0f: DisplayPort-1 10: DisplayPort-2
  • 2,611
1 vote
Accepted

Unix. Run script across multiple dirs on specific files, where pathname has regex

/mypath/MAP-9-[0-9][0-9][0-9]/*.bam is a shell glob, or filename expansion expression. It expands to a list of matching files - you can use that to iterate over your input files, but you can't expect ...
  • 76.1k
0 votes

Extract lines from indented output

Using awk: sudo ddcutil capabilities | awk ' /Feature: 60/{ f=1; next } f { if (NF==2) print $1, $2; else if ($1=="Feature:") exit }'
  • 24.8k
1 vote

Extract lines from indented output

#!/bin/perl while(<>) { if (/Feature: 60/) { while(<>) { last if (/Feature/); next if (/Values/); print $_, "\n"; } } } Easy :)
  • 3,807
3 votes

Unix. Run script across multiple dirs on specific files, where pathname has regex

Don't be confused by glob vs regex (you use glob here): Globs are shell patterns that can be used for matching strings or expanding pathnames: [[ $name = Bob* ]] rm *.txt See http://mywiki.wooledge....
0 votes

Non-greedy match with SED regex (emulate perl's .*?)

It doesn't appear that typical/vanilla sed supports non-greedy RegEx repetitions (aka Minimal Repetitions), so our solution cannot rely on that if portability matters. With that sed however (pun ...
  • 5
0 votes

How can I rename files recursively using a regular expression

Not really answering the how-to of the question but looking beyond that to the reason, on systems with GNU ls you can use ls -v to list the files in "natural" order. Here's what the ...
  • 101k
1 vote

Print a line matching pattern1 unless next line contains pattern2

As in life, in software it's much easier to take action based on what has happened in the past (i.e. data you've read) rather than based on what will happen in the future (i.e. data you haven't read ...
  • 27.1k
6 votes
Accepted

Using Bash "=~" operator with WHILE instead of UNTIL loop

until cmd is the same as while ! cmd. Here, just do: until IFS= read -rep 'Enter domain name: ' domain [[ $domain =~ $regexp ]] do printf>&2 'Invalid domain: "%s"\n' "$...
0 votes

Find two lines with unknown number of lines between them

To do what you asked for using any awk would be: awk ' /^file_[0-9]+\.json$/ { printf "%s", rec rec = "" } { rec = rec $0 ORS } /document\(s) ...
  • 27.1k
1 vote
Accepted

Remove and add at the same time with sed

Correcting the code block somewhat, read -p "Enter the name of city: " city if [ "$city" = 'Liverpool' ] then sed -Ei 's/^#* *(\$type_of_city) *= *.*/\1 = '"$city"'/' ...
  • 101k
1 vote

Remove and add at the same time with sed

In your case, I would replace the entire line with sed, where the # is anchored as optional character right after start-of-line: sed "s/^#\{0,1\}\$type_of_city =.*/type_of_city = $city/" /...
  • 21k
0 votes
Accepted

Find two lines with unknown number of lines between them

If there are no blank lines inside each block of text, then you could use sed to insert a blank line after each line with imported successfully and then process the file in "paragraphs" (...
  • 73.2k
5 votes
Accepted

Print a line matching pattern1 unless next line contains pattern2

grep or perl -n works on one line at a time so the thing the regexp matches on is just the contents of one line (with the line delimiter not even included with grep or perl with -l). You could use ...
1 vote

Print a line matching pattern1 unless next line contains pattern2

With grep: (regex hardened with word boundaries) grep -Pzo '(?m)\bfoo\b(?!.*\n.*\bbar\b)' file foo -P is pcre mode -z is NULL \0 delimited to enable parsing all lines as a string (\0 is meant for end ...
0 votes

Find two lines with unknown number of lines between them

Using awk: awk -v startblock='^file_[0-9][0-9][0-9][0-9]\\.json$' \ -v endblock='document\\(s\\) failed to import\\.$' ' $0 ~ startblock { error=0 s="" } { ...
  • 24.8k
0 votes

Find two lines with unknown number of lines between them

I tried with the following text file with output, file_0108.json 2023-02-22T01:15:05.531+0000 connected to: mongodb://[**REDACTED**]@localhost 2023-02-22T01:15:08.531+0000 [######...................
  • 5,941

Top 50 recent answers are included