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 ...
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"....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 {
...
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-...
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 [ ! ...
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/(?:[.]([...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
awk × 8652text-processing × 2914
sed × 2816
grep × 1084
shell-script × 980
bash × 971
linux × 848
shell × 474
perl × 447
csv × 365
regular-expression × 342
scripting × 280
command-line × 185
text-formatting × 174
cut × 148
files × 147
sort × 134
join × 132
columns × 126
bioinformatics × 119
find × 91
string × 87
numeric-data × 84
date × 80
logs × 74