New answers tagged

1

perl -00 -n -e 'print if (m/blue/i && m/green/i && m/yellow/i)' filename This uses perl's paragraph-reading mode (-00) to print only paragraphs containing all three words (with case-insensitive matches). a 'paragraph' is one or more lines of text, separated from other paragraphs by at least one blank line. e.g. I saved the text of your ...


5

Eric Blake answered on the bash-bugs mailing list: jobs is an interesting builtin - the set of jobs in a parent shell is DIFFERENT than the set of jobs in a subshell. Bash normally creates a subshell in order to do a pipeline, and since there are no jobs in that subshell, the hidden execution of jobs has nothing to report. Bash has code to ...


0

To remove all leading and trailing spaces from a given line thanks to a 'piped' tool, I can identify 3 different ways which are not completely equivalent. These differences concern the spaces between words of the input line. Dependanding on the excepted behaviour, you'll make your choice. Examples To explain the differences, let consider this dummy input ...


1

In general I would recommend the answer by Gilles. If you need your script to be POSIX compatible and your ps does not support -u, this could work: ps -ef | grep -e \ "^smtpd ^dbus ^ntp" I.e. pipe the output of ps -ef through grep -e pattern_list. The pattern_list is supplied as a multiline-string, each pattern starts with ^ to anchor it at the ...


3

Short answer: ps -u user1,user2,user3 Your grep command is incomplete since the -f option requires an argument. Your cat command is receiving its input from the here document, so even if you fixed the grep command, its output would be discarded. If you were looking for a hard-coded user, you'd use ps -ef | grep alice (except that this isn't a good way ...


1

You did not specify the shell, so, in general, named pipes would be easiest way. However, if your shell supports them, this could be a nice use case for coprocesses. How do you use the command coproc in Bash? In a | cmd | b, a feeds data to cmd and b reads its output. Running cmd as a co-process allows the shell to be both a and b. In bash: coproc ...


4

As @muru commented, the problem is that your script does not handle SIGPIPE. The following changes will allow the script to terminate as intended: function signalHandler() { echo "sig: $1 received ==> exit" >&2 # <-- for i in {1..5}; do echo "cleanup $i %" >&2 # <-- sleep 1 done trap SIGINT ...


0

Likely a incomplete implementation https://www.cygwin.com/ml/cygwin/2016-01/msg00085.html


0

You can use Vim in Ex mode: ex -sc '%s/OLD/NEW/g|x' messages.txt % select all lines s substitute g global replace x save and close


2

How about you just print all the files that don't match your extensions? find documents -type f ! \( -name \*.txt -o -name \*.doc -o -name \*.docx \) find media -type f ! -name \*.gif find pictures -type f ! \( -name \*.jpg -o -name \*.jpeg \) Why do you need to check other at all if anything is allowed in there? By the way, Unix convention is: ...


0

You don't. Pipes are unidirectional: you write at the write end, and read from the read end. If you want bidirectional communication, use a socket, or two pipes. You can use a pipe for bidirectional communication if both processes keep both ends of the pipe open. You need to define a protocol for whose turn it is to talk. This is highly impractical.


4

Just a quick hack: when grep is sending output to a pipe, it also commutes to no-changing-color mode x | grep hello | cat


11

You could do this, x | grep --color=never hello To quickly test it, you can do, ls -l /etc/ --color=always | grep --color=never .


0

Your make_folder function is needlessly complicated: all these current directory changes are just making your life more complicated. On top of that, you don't need sudo: why would you create the directories as root? Just call mkdir. mkdir documents other pictures media If you want the script to work even if the directories already exist, use mkdir -p: ...


1

From the top of my head I would use the 'case' statement in the end. i.e. case "$FILE" in *.jpg|*.jpeg) mv "$FILE" to where you want it ;; *.gif|*.mov) mv "$FILE" to where you want it ;; *) echo "Unmanaged file type: $FILE, skipping" ;; esac ...however you need to wrap it in a loop container, ...


2

How about using read? $ cat /dev/null | read pointless || echo no output no output $ echo something | read pointless || echo no output $ printf "\n" | read pointless || echo no output $ printf " \n" | read pointless || echo no output $ false | read pointless || echo no output no output According to the Open Group definition: EXIT STATUS The ...


3

Parsing whitespace-delimited columns is something awk does well. canceljob $(showq | awk '$2 == "jquick" {print $1}') or more directly showq | awk '$2 == "jquick" {system("canceljob " + $1)}' Alternatively, pass a constraint to showq to make it report only the desired jobs. canceljob $(showq -w user=jquick)


3

try showq | awk '/jquick/ { printf "canceljob %d\n",$1}' if OK, pipe to bash, or showq | awk '/jquick/ { print $1}' | xargs canceljob


1

Essentially what you are trying to do is logical AND. grep is a line matching tool, so in terms of a line patter1 AND patter2 logic mean they are going to be on the same line ; when you search a file , where you want to find line with one term and another on the same line, you can use regular expression grep 'pattern1.*pattern2' input_file.txt $ ps -ef | ...


6

Pipelines run from left to right. More precisely, the processes run in parallel, but the data flows from left to right: the output of the command on the left becomes the input of the command on the right. Here the command on the left is grep tool. Since you're passing a single argument to grep, it's searching in its standard input. Since you haven't ...


3

You can use grep in different ways, for instance: cat myfile | grep keyword This above example will dump the file to stdout. The pipe character | will take that output and feed it to grep keyword command as stdin. In the end, the lines in myfile containing keyword will be written to the stdout, which is generally terminal screen. This is a very ...


3

This command is going to search for foo in the file filename and in the results for bar: grep foo filename | grep bar An alternative would be with awk: awk '/foo/ && /bar/' filename


0

You can use Vim in Ex mode: ex -sc '%!shuf' -cx example.txt % select all lines ! run command x save and close


3

tar: option requires an argument -- 'f' This gives it away -- tar's 'f' flag specifies the file to read or create. Since you were piping a (compressed) tar file in from curl, you just needed to tell tar that the "file" to read was stdin with a '-'.


0

Per this answer provided in the comments by @thrig I was able to get it to output correctly by doing yum list installed | xargs -n3 | column -t | tee /tmp/yum-list-installed


3

Probably because netcat exits shortly after getting an end-of-file on its standard input.  Try (echo 'HEAD /'; sleep 5) | nc 172.17.0.1 3128 to keep netcat's local input open long enough for it to read the network data and write it to the standard output.  The 5 is the number of seconds to delay. Netcat has an option to deal with this. If you specify a ...


1

Try the sed -u (-lon BSD/Mac OSX systems), and grep --line-bufferedoptions.


1

You seem to expect the out2 and out3 being written in real time, but sed and grep in pipe wait for the EOF. And nothing fails here. Kill openssl from another console and check if you have correct results in out2 and out3.


1

When you give an explicit file name, such as toto.wav, SoX will deduce from the .wav extension that it is supposed to use WAV format. In case of - being the output “file name”, that deduction can’t be done, so you have to specify the type explicitly with -t wav. The same would apply if you wanted to give the file a different extension (toto.sound) or none at ...


1

You need to declare the type of the sox output by adding -t wav before the second -. When it's a file name, sox peeps at the name and deduces the type from there, but when it's stdout, the type needs to be declared. You might also want to declare all other settings as well (-b 16 -e signed -c 1) rather than assuming they are transferred from the input; all ...


1

Based on your example it looks like you're looking for a way to group arguments to operations with lower precedence than |, (i.e. && and ||). The { and } metacharacters can be used for grouping in this way. Your example above could be written like this (using the exit status of which to determine whether cmd2 exists or not). cmd1 | { which cmd2 ...


4

You can write a mini function to make it more concise: ifexists(){ if command -v "$1" then "$@" else echo "doing cat for missing $1" >&2 cat fi } echo hi | ifexists mycommand | cat -n



Top 50 recent answers are included