10
votes
Accepted
Copy an evenly spaced subset of files
Note that brace expansion is not globbing, it will expand regardless of whether words in the result refer to actual files. If you want to copy only those of those files that actually exist, you ...
8
votes
Two ssh output as awk input
Beware of those curly quotes (“ and ”) some Windows text editors will use, use straight quotes instead (' or "). You should also be using 's, unless you have some reason to use "s, e.g. to ...
7
votes
Accepted
Two ssh output as awk input
You need to group the commands. The simplest way is to use curly braces which group them without creating a subshell:
{ ssh username@host1 'cat /tmp/test/*'; ssh username@host2 'cat /tmp/test/*'; } |
...
7
votes
Copy an evenly spaced subset of files
Brace expansion in bash supports steps in the format {<start>..<end>..<step>}:
$ echo file{0..100..19}.txt
file0.txt file19.txt file38.txt file57.txt file76.txt file95.txt
Though it ...
5
votes
Accepted
Print Variable containing backslashes
If printf '%s' "$A" doesn't show any backslash then they're not there. You can check with curl https://mysite.com alone.
Maybe you were confused by the output of:
bash-5.0$ typeset -p A
...
5
votes
How can I reformat blocks of data until the end of the file is reached?
Using any awk whether that 3 number of lines in a block can vary or not:
$ awk '
NR == 2 { $3=""; saved=$0; next }
NR == 3 { $0=saved $3 }
NR < 4 { print; next }
!...
3
votes
Two ssh output as awk input
One possible way is to exec ssh command in subshell and pipe the result on awk:
(ssh username@host1 "cat /tmp/test/*"; ssh username@host2 "cat /tmp/test/*" ) | awk ‘ command ‘
2
votes
How can I reformat blocks of data until the end of the file is reached?
Using Raku (formerly known as Perl_6)
Use skip to forget about header lines for the moment:
~$ raku -e 'my @a = lines.skip(3).rotor(4, partial => True).map: *.words; .[0,3,5,7].put for @a;' file
#...
2
votes
How can I reformat blocks of data until the end of the file is reached?
Quick and dirty anwser -- feel free to run shellcheck on this:
#!/usr/bin/env bash
# Reformat comments:
read -r first_line
echo "${first_line}"
read -r sharp line2_word1 line2_word2
read -r ...
2
votes
Copy an evenly spaced subset of files
There isn't a useful pattern in the set of file names (numbers) you've provided, so the simplest "one liner" you can hope for is:
cp file0.txt file19.txt file39.txt file59.txt file79.txt ...
2
votes
Accepted
extract specific filenames from a zip without extracting it
That's a bit of a convoluted specification. If I understand correctly, you want the list of archive members whose path does not contain deployfiles (unless they also contain dev case insensitively), ...
2
votes
How can I reformat blocks of data until the end of the file is reached?
Using AWK:
$ awk '
NR==2{sub(/[[:space:]]+[^[:space:]]+$/,"");rec = $0; next}
NR==3{$0 = rec OFS $NF};
NR<4;
NR>3{printf "%s", ...
2
votes
In a shell script, how do I print the input of every step of a pipe chain?
If you want them to not be confused and you only need to debug one pipeline at a time you could use tee to pipe a copy of each output to a file. In your case:
tree --noreport -nFif -I $...
2
votes
Bash - Check if the standard input contains anything
[[ ! -t 0 ]] = Does standard input contains anything?
Your premise is wrong. This tests whether stdin is connected to a terminal/tty, not whether stdin contains anything. Because of the negation the ...
1
vote
How can I reformat blocks of data until the end of the file is reached?
With the caveats mentioned in your question and using your sample input as file q762948, you can do this by a simple awk command:
$ head -2 q762948 >result.txt
# dump the comments as required
$ ...
1
vote
Accepted
Find file in directories of PATH env which match partially
This should do the work:
compgen -c | grep 'YOUR_SEARCH_STRING'
Example:
compgen -c | grep '\-linux'
Hope that helped :)
1
vote
Matching negative patterns with bash extglob
I'd say the second expression fails because item* matches the entire filename for each of these files, and the empty string at the end trivially matches !(more).
1
vote
How to turn off the ANSI escape sequence of Bash?
Just tell that you have a terminal which do not support such features, e.g. putting
TERM=vanilla jest foo.js
or set the environment variable at beginning of your script (remember to export it: the ...
1
vote
Accepted
Identifying specific set of characters in binary file name
Copy all files in the current directory that contain the substring A1 in their names to dirA (a directory that is assumed to exist), and then copy all files with B1 in their names to dirB in the same ...
1
vote
How to prepend a prefix to variables sourced from a config/env file
A slight addition to @xhienne's answer, with a regexp that will recognize # at beginning-of-line as a comment, and some QED exercises showing that both sourcing and exporting work using this techique.
...
1
vote
How to prepend a prefix to variables sourced from a config/env file
Here is a naive solution using sed:
source <(sed 's/[^=]\+=/export my_app_&/' local.env)
I call this "naive" because I assume that each line containing an equal sign is a variable ...
1
vote
Copy an evenly spaced subset of files
If your system has jot installed, it can do this:
$ jot -w 'cp "file%d.txt" "destination/"' 5 0 100
That command tells jot to generate 5 numbers between 0 and 100, inclusive. ...
1
vote
shell .sh script does not execute all tasks within, except when run manually
This is more of a comment than an answer but there are only so many lines available in a comment
It's not at all clear what you're trying to achieve with your situation. You've explained that it's ...
1
vote
Why .profile and .bash_profile have non-owner R permissions?
First, the default profile files do not contain passwords and typically the information in these files is not sensitive, and it is rare for sensitive information to go in them and they typically don't ...
1
vote
bash - Is it possible to capture the output of several background commands without writing to disk?
You are looking for parset (part of GNU Parallel).
parset result1,result2,result3 'pipeline | number | {}' ::: one two three
parset is built on top of GNU Parallel, which has been field tested for ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
bash × 27091shell-script × 6790
shell × 4110
linux × 2799
scripting × 1521
awk × 984
command-line × 959
sed × 944
terminal × 786
text-processing × 701
grep × 699
ssh × 689
find × 649
variable × 624
ubuntu × 571
io-redirection × 570
zsh × 561
pipe × 548
files × 533
environment-variables × 522
quoting × 498
alias × 497
wildcards × 449
command-history × 445
regular-expression × 425