Hot answers tagged

4 votes
Accepted

How to use awk's gensub or alternatives to replace overlapping matches

You could do something like: perl -pe 's{\.([a-z](?:\.[a-z])*)\.}{"[$1]" =~ s/\./][/gr}ge' That is replace all the .x.y.z. with [x.y.z] inside which the .s are substituted with ][. The same ...
Stéphane Chazelas's user avatar
3 votes
Accepted

Add list of words at the end of the 1st line in a loop

To store a list, you want an array: a=( file1 file2 'other file with spaces' $'even with\nnewlines' ) awk -- ' FNR == 1 { close(out) $0 = $0 FILENAME out = FILENAME"....
Stéphane Chazelas's user avatar
3 votes
Accepted

Extracting text preceding a particular string

This truncates the lines of input at the first occurrence of the string .nc: sed 's/\.nc.*//' (Note that the dot in .nc must be escaped as it would otherwise match any single character. The .* after ...
Kusalananda's user avatar
  • 316k
2 votes
Accepted

Linux bash awk print word with special letter

Something like this works ... there may be more efficient approaches: echo 'www.google.com/word/word1/word2/word3/word_4' | awk -F'/' '{for(i=2;i<=NF;i++){if($i~/_/){print $i}}}' word_4 We just ...
tink's user avatar
  • 6,111
2 votes

How to use awk's gensub or alternatives to replace overlapping matches

Usually regex engines don't consider overlapping matches, not the way you propose, but also not such that a latter match would consider characters inserted by a previous replacement. In Perl, you ...
ilkkachu's user avatar
  • 130k
1 vote

Linux bash awk print word with special letter

A simple solution would be just: % echo 'www.google.com/word/word1/word_2/word3/word4' | tr -s '/' '\n' |grep _ word_2 That is, change slashes to newlines, then print the resulting lines that ...
ilkkachu's user avatar
  • 130k
1 vote

Extracting text preceding a particular string

I suspect what you really want is to find .nc either followed by . or at the end of the line, and that you want to match on the first occurrence of that (as opposed to the last occurrence) on each ...
Ed Morton's user avatar
  • 27.9k
1 vote

Zero pad file names using find and execdir flag

To zero-pad to length 4 for instance the number at the start of all mp4 and sth file, with zsh: autoload -Uz zmv zmv '(**/)(<->)(*.(mp4|sth))' '$1${(l[4][0])2}$3' Note that -regex, in the find ...
Stéphane Chazelas's user avatar
1 vote

Join strings on different lines depending on indentation with awk

Assuming GNU gawk ... No fault proofing included though(Taking your word for: "that is the literal document") ... So,: $ cat file foo foobar bar baz bat bar $ $ gawk 'BEGIN { ...
Raffa's user avatar
  • 166
1 vote

Join strings on different lines depending on indentation with awk

With perl: <your-file expand | perl -lpe ' ($indent, $txt) = /^( *)(.*)/; $depth = length($indent) / 2; $part[$depth] = $txt; $_ = join ".", @part[0..$depth]' Or golfed: <your-...
Stéphane Chazelas's user avatar
1 vote

Add list of words at the end of the 1st line in a loop

Assuming all the files are in the same directory and the list is in a file: files.txt file1 file2 Using mapfile and sed: mapfile -t files < files.txt for f in "${files[@]}"; do if [ ! ...
schrodingerscatcuriosity's user avatar
1 vote

How to use awk's gensub or alternatives to replace overlapping matches

Here is a ruby: echo ".a.b.c." | ruby -pe '$_.gsub!(/(?:[.][a-z](?=\.))|\./){|m| m[/^\.$/] ? "" : "[#{m[1]}]" }' Or Perl: echo ".a.b.c." | perl -pe 's/(?:[.]([...
drewk's user avatar
  • 211
1 vote

Read from and append to file at the same time while preserving end-of-line

Your main problem is that your forgot to quote $(echo...) and $ESCAPED therefore invoking the split+glob operator, where the expansion is split according to characters of $IFS and the resulting words ...
Stéphane Chazelas's user avatar

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