New answers tagged

1

I wanted to use a standard input (pipe) in case a resulting command exceeds a command line length limit. The following command worked for me: echo "../smth*/*" "/etc/cron*/" | xargs -n1 -I{} bash -O nullglob -c "echo {}" | xargs -n1 or for a list of globs: cat huge_glob_list.txt | xargs -n1 -I{} bash -O nullglob -c "echo {}...


-1

So for future beginners out there: *.txt expands before being evaluated by cp. That is why it only took one argument.


0

Here's a solution using bash arrays and grep: #!/usr/bin/env bash declare -a pats pats=('pat1' 'pat2' 'pat3' 'pat4') # find the width of the widest pattern: wid=0 for pat in "${pats[@]}" do if [[ ${#pat} > $wid ]]; then wid=${#pat}; fi done # The above might give misleading widths if your patterns are # regexes that are signifcantly ...


1

Using Raku (formerly known as Perl_6) raku -e 'my @a; my @pat = <<pat1 pat2 pat3 pat4>>; for lines() { for @pat -> $i { @a.push( m[$i] // " __ " ) };}; .put unless $_ eq " __ __ __ __ " for @a.rotor(4);' Answering in Raku because of a niggling question: what if a line contains more-than-one desired pattern? Or any ...


0

While trying to adapt @EdMorton's answer to my problem I found the info that I needed from an older thread where he contributed with an answer and managed to solve the problem entirely. here is my solution: awk '/pat1/{v1=$4; next}{v1="xxx"} /pat2/{v2=$5; next}{v2="xxx"} /pat3/{v3=$6;next}{v3="xxx"} /pat4/{v4=$5}{v4="xxx&...


1

It seems like you'd need something like: $ cat tst.awk BEGIN { OFS=" " } { sub(/\r$/,"") } ( ($NF ~ /pat1/) && (state == 0) ) || ( ($NF ~ /pat2/) && (state == 1) ) || ( ($NF ~ /pat3/) && (state == 2) ) || ( ($NF ~ /pat4/) && (state == 3) ) { v[++state] = $NF } state == 4 { print v[1], v[2], v[...


0

Perhaps just use the END clause to print out your results. awk '/pat1/{v1=$4; next} /pat2/{v2=$5; next} /pat3/{v3=$6; next} /pat4/{v4=$5;} END{ print v1," ",v2," ",v3" ",v4 }' myfile.out


0

You didn't explicitly say if you want the matching files in those two places only, or all of them in the tree. In case matching all of them is an option, you could use **: % mkdir -p foo/{bar,asdf/asdf,}/baz % echo foo/**/baz foo/asdf/asdf/baz foo/bar/baz foo/baz it works in Bash too (with globstar enabled): $ shopt -s globstar $ echo foo/**/baz ...


2

It is a bit of a pain point with ksh and zsh globs. You can use braces: echo foo{,/bar}/baz echo foo{,/*}/baz # you probably don't want this one, see below However, braces are not globs, and whether they're close enough depends on exactly what you're doing. The main issue is which files are guaranteed to exist and what you want to happen if some files ...


1

If it doesn't matter to you if you accidentally match >1 of the "optional" pathname component, you can do: % set -o EXTENDED_GLOB % echo foo/(bar/)#baz This will match foo/bar and foo/bar/baz, but it will also match foo/bar/bar/baz, etc.


19

Bash did expand the star in your first example. Assuming that the current directory contains only one file, main.py, here is what happened: $ seq 3 | xargs -I * echo * # Expands to: $ seq 3 | xargs -I main.py echo main.py This asks to do echo main.py on each input, replacing main.py with that input (because of -I main.py). And this is what you observed: ...


1

I think that you can do as cute as the %.mobi: %.epub from the GNU make part of the accepted answer --without GNU make and its filenames limitations-- with a little wrapper: from_to(){ sp=${1%%%*}; ss=${1#*%}; shift dp=${1%%%*}; ds=${1#*%}; shift for s in "$sp"*"$ss"; do d=${s#"$sp"}; d=$dp${d%"$ss"}...


3

using bash... find . -name '*.epub' | xargs basename -s .epub | xargs -I {} ebook-convert {}.epub {}.mobi ; or, for filenames with spaces... find . -name '*.epub' -print0 | xargs -0 basename -s .epub | xargs -I {} ebook-convert "{}.epub" "{}.mobi" ; breaking it down... find . -name '*.epub' find . -name '*.epub' | xargs basename -s ....


6

Can you use wildcards in a similar way, that works for a scenario like the above? Not like ebook-convert *epub *mobi, because of how wildcards - really "shell globbing" - works. But, you can get started with a wildcard. Shell globbing is, conceptually, very straightforward: find all of the files that match the glob and replace the glob with that ...


8

With the zsh shell: for f (./*.epub) ebook-convert $f $f:r.mobi With $f:r expanding to the rootname (the part without the extension) of $f like in csh/vim... Or: autoload -Uz zmv # best in ~/.zshrc zmv -P ebook-convert './(*).epub' './$1.mobi` (as ebook-convert doesn't seem to recognise the -- option delimiter, we have to use a ./ prefix and use -P instead ...


27

You can’t do it directly with wildcards, but a for loop can get you there: for epub in ./*.epub; do ebook-convert "${epub}" "${epub%.epub}.mobi"; done Zsh supports a more elegant form of this loop. Instead of a shell script, if your file names don’t contain whitespace characters, and more generally can be safely handled by Make and the ...


Top 50 recent answers are included