Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

New answers tagged

0 votes

Get the last word on each line

Using Raku (formerly known as Perl_6) raku -ne '.words[*-1].put;' OR raku -ne '.words.tail.put;' Command line flags -ne to run the code on each line of the file. The .words call breaks on ...
user avatar
  • 1,108
0 votes

Get the last word on each line

The standard idiom for printing the last field on a line is awk '{print $NF}' The NF variable is automatically set to the Number of Fields on the line, and then $ extracts that field. I'd say the ...
user avatar
4 votes

Get the last word on each line

You can easily use grep with option -o (short form of --only-matching). grep -o "\w*$" filename \w matches any word character (alphanumeric and underscore) \w* matches multiple (including ...
user avatar
2 votes

Get the last word on each line

Using GNU grep and tail with perl-style regex grep -Po '.* \K.*' file | tail -12 unshare access lstat futex epoll_wait fadvise64 read lstat stat fallocate access open grep -o '[^] ]*$' file | tail -...
user avatar
  • 1,119
6 votes

Get the last word on each line

Or like so: awk 'NR>2 {print $NF}' seccomp unshare access . . . which, for lines beyond the second, prints the last field of the line. NF holds the number of fields, $NF "expands" to the ...
user avatar
  • 8,340
4 votes

Get the last word on each line

With sed it would be simply sed '1,2d;s/.* //' 1,2d mean to delete the first to second line, replacing the tail the substitute command removes everything up to the last whitespace, so you don't need ...
user avatar
  • 11.2k
0 votes
Accepted

bash + verify if file that ended with many combinations exists

I would use either find or a for loop to identify this situation. Example #1 with find (using GNU extensions to limit the search space): # First try with no matching files [ -n "$(find /tmp/file* ...
user avatar
  • 89.2k
1 vote

sed - group does not match when including bracket

This answer focus on GNU sed. In the gnu manual of sed theres's an explanation about the regular expression syntax and use: 5.2 Basic (BRE) and extended (ERE) regular expression Basic and extended ...
user avatar
0 votes

Remove extra characters from diff output

Something like in awk can do the work: diff -y --suppress-common-lines backup.txt newfile.txt|awk -F'[>,]' '{gsub(/ /,"")} {print $2} '
user avatar
-1 votes

Need assistance with writing a bash script that uses regex to validate that password input meets length and special character requirements

As an option: if [ ! "${password/????????*}" -a\ "$password" != "${password/[0-9]}" -a\ "$password" != "${password/[#?!@$ %^&*-]}&...
user avatar
  • 2,103
0 votes

Remove extra characters from diff output

You could tell diff to give you just that with: diff --old-line-format= \ --unchanged-line-format= \ --new-line-format=%L backup.txt newfile.txt To post-process the diff -y output, you ...
user avatar
5 votes
Accepted

Need assistance with writing a bash script that uses regex to validate that password input meets length and special character requirements

The extended regular expression syntax supported by bash lack the ability to construct single expressions that perform boolean AND tests between several subexpressions. Therefore, it would be easier ...
user avatar
  • 268k
1 vote

Rename files to order them using perl substitution

With zsh instead: autoload -Uz zmv to autoload the zmv function (best in ~/.zshrc). Then: zmv -n '(fine_)(<->)(.vtu)' '$1${(l[5][0])2}$3' (-n for dry-run).
user avatar
3 votes
Accepted

Rename files to order them using perl substitution

It really depends on the version of rename that you have. Here's what my version reports: rename Usage: rename [-v] [-n] [-f] perlexpr [filenames] Please be aware, though, that the default rename ...
user avatar
  • 89.2k
0 votes

How to match all lines sharing same words

Using GNU sed ##> regex for Mac address h2='[[:xdigit:]]{2}' mac_addr="$h2(:$h2){5}" sed -En " /^(Mamba|),$mac_addr,/G /^[^,]*,($mac_addr),.*\n\1\n/ba /^Mamba,.*\n/!d h s/^...
user avatar
  • 5,343
2 votes
Accepted

How to match all lines sharing same words

my understanding is get all line with Mamba, remember mac get all line with those mac I came with this awk awk -F, '$1=="Mamba" { m[$2]=NR ; } $2 in m { print ;}' where -F, use , as ...
user avatar
  • 29.3k
1 vote
Accepted

Not obtaining lazy evaluation using sed

Most versions of sed don't support perl style regular expressions (the only one I know of that does is "super sed", ssed, and that hasn't been updated since 2005), and don't support the ? ...
user avatar
  • 66.7k
4 votes
Accepted

-bash: syntax error near unexpected token `(' when using lookahead and lookback?

The issue that the error message addresses is not the single quotes but the parentheses. Unquoted parentheses are special to the shell, and their meaning depends on their placement on the command ...
user avatar
  • 268k
1 vote

Rookie Question: Regex in ZSH Script

zsh has some builtin support for regexp matching (both ERE and PCRE), it also has extended glob operators that go beyond regexps. No need to use that antiquated and broken by design expr command (...
user avatar
0 votes

Rookie Question: Regex in ZSH Script

expr output is the number of characters that matched, did you try printing paperno variable ? I don't think it gives you the spected output https://www.computerhope.com/unix/uexpr.htm May be the ...
user avatar
  • 1
2 votes
Accepted

Return content between corresponding brackets

What you've offered isn't valid JSON. Bracketing the expression, fixing up the other errors, and adding a counter-example: { "text": [ { "string1": ["...
user avatar
  • 89.2k
0 votes

sscanf equivalent in shell script

You can try this with awk awk -F': ' -v OFS="," '{split($1,a," "); split($2,b,","); print a[1],a[2],b[1]}' input_file This will put in variable a[1] the first zero, in a[...
user avatar
1 vote

How to search filenames by regex with "find" and rename this files

With zsh: autoload zmv zmv -n '(**/)*-(<->-<->.mp4)' '$1$2' (remove the -n for dry-run if happy).
user avatar
1 vote
Accepted

How to search filenames by regex with "find" and rename this files

GNU find supports several regular expression dialects (the -regextype option lets you select one). Parentheses are special characters in some of them and need to be backslash-escaped to gain their ...
user avatar
  • 8,991
1 vote

There must be a better way to replace single newlines only?

Using Raku (formerly known as Perl_6) raku -e '.put for slurp.subst(:global, / \N <(\n)> \N /, " ");' OR raku -e '.put for lines.join("\n").subst(:global, / \N <(\n)> ...
user avatar
  • 1,108
0 votes

How to recursively change names of files to match?

You can use your find statement in a for loop and use shell substring replacement. Something like this. for file_name in $(find . -name '*1.txt' -print0); do #echo mv -v "$file_name" &...
user avatar
  • 1,286
1 vote

match number range with non significative zeros in zsh

If it's matching you want, as opposed to generating a list of files regardless of whether they exist or not, use the <x-y> glob operator: print -rC1 -- booklet-<79-256>.pdf <79-256> ...
user avatar
3 votes
Accepted

How to get the matched expression in GNU Recutils?

The ~ operator is a comparison operator: it answers the question, "does the string on the left match the expression on the right?", and it returns a boolean value as a result. So when you ...
user avatar
  • 24.2k
0 votes
Accepted

I'm trying to understand how to shorten regex

Well, [...] is a bracket group, it matches a single character that's any of the ones listed inside, obeying ranges (in some way that depends on the locale). [0-9\.] matches any digit from zero to nine,...
user avatar
  • 115k
0 votes

match number range with non significative zeros in zsh

Simply do in zsh: ls booklet-{079..256}.pdf Test it, it works!
user avatar
  • 930

Top 50 recent answers are included