Hot answers tagged

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 ...
Stéphane Chazelas's user avatar
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 ...
Ed Morton's user avatar
  • 30.2k
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/*'; } | ...
terdon's user avatar
  • 239k
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 ...
muru's user avatar
  • 71.3k
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 ...
Stéphane Chazelas's user avatar
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 } !...
Ed Morton's user avatar
  • 30.2k
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 ‘
Romeo Ninov's user avatar
  • 17.1k
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 #...
jubilatious1's user avatar
  • 2,603
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 ...
Xavier G.'s user avatar
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 ...
Chris Davies's user avatar
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), ...
Stéphane Chazelas's user avatar
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", ...
Prabhjot Singh's user avatar
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 $...
davolfman's user avatar
  • 564
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 ...
Chris Davies's user avatar
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 $ ...
elmo's user avatar
  • 174
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 :)
Pixelbog's user avatar
  • 661
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).
muru's user avatar
  • 71.3k
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 ...
Giacomo Catenazzi's user avatar
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 ...
Kusalananda's user avatar
  • 327k
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. ...
Jim L.'s user avatar
  • 7,399
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 ...
xhienne's user avatar
  • 17.6k
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. ...
Jim L.'s user avatar
  • 7,399
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 ...
Chris Davies's user avatar
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 ...
user10489's user avatar
  • 6,253
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 ...
Ole Tange's user avatar
  • 34.8k

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