Tag Info

New answers tagged

0

sed '/[^0-9]/d;s/.$/&p/' <input.txt | sed -nf - file That's two seds working together. The first makes some small attempt at ensuring it only edits appropriate lines by refusing to print any line that contains a single non-numeric character and only editing any lines containing at least one character. Basically its job, though, is just to transform ...


1

awk 'NR==FNR{linesToPrint[$0];next} FNR in linesToPrint' line-numbers.txt file.txt


0

xargs is the unix utility I was looking for. From the man page: The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments. Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from ...


2

less normally refuses to open non regular files like pipes or also binaries. You can use the -f operator to force less to open non regular files: less -f <(command) Another approach is to use process substitution: less < <(command) This causes the pipe that was created with <() to act as standard input (STDIN) for less.


1

less tends to complain /dev/fd/63 is not a regular file The solution to that, under GNU less at least, is the use of the -f option: less -f <(command) Under normal circumstances, less will not open non-regular files (stdin excepted, obviously). This is for your protection. -f forces non-regular files to be opened.


0

Another perl solution: $ perl -MList::Util=any -nle ' BEGIN { open $fh, "<", "input.txt"; @lines = <$fh>; close $fh; } print if any { $_ == $. } @lines; ' file


0

Combine sed with xargs and printf: sed -n $(xargs printf "%sp;" < input.txt) data -n tells sed not to print lines unless you tell it to explicitly. xargs runs a command with the lines from standard input as its arguments. printf will format each argument as described. The $(...) above expands to (for your sample file): 2088p;2089p;2095p;2096p; which ...


1

Assuming michas' interpretation is correct, here's an awk solution: awk 'FNR==NR{a[i++]=$0} # Process the first file FNR!=NR{ # Process the second file for (i in a){ if(FNR==a[i]){ print $0 } } }' file_with_line_numbers other_file In Perl: perl -E ' while(<>){ ...


0

If I understand your problem correctly something like this should work: for i in $(cat numbers.txt); do cat lines.txt|tail -n +$i|head -n 1; done For each of the numbers in file "numbers.txt" it extracts the corresponding line of the other file and prints it. The same thing with sed and xargs looks like this: xargs -i sed "{}q;d" lines.txt ...


2

Just pipe through a while loop: git diff --name-only develop | grep coffee$ | while IFS= read -r file; do ./node_modules/.bin/coffeelint "$file" done


3

Yes, close can block: If O_NONBLOCK is not set and there have been no signals posted for the STREAM, and if there is data on the module's write queue, close() shall wait for an unspecified time (for each module and driver) for any output to drain before dismantling the STREAM. And: If fildes refers to a socket, close() shall cause the socket to be ...


2

If you know your output will always be in a format of X lines of header and Y lines of footer, you can use head and tail to get only the part you need, echo query | impala-shell | tail -n +X | head -n -Y # -- in your case above -- echo "select * from abc where key > 'a-26052014015400' limit 1;" | impala-shell \ | tail -n +13 | ...


3

try to pipe it to grep: $ grep -E "| a-[0-9]* | HS2 | [0-9]* | [0-9]* |" to get rid of the first | and the last |: $ grep -Eo " a-[0-9]* \| HS2 \| [0-9]* \| [0-9]* " "-E" to access the extended regular expression syntax "-o" is used to only output the matching segment of the line, rather than the full contents of the line.


0

To finish Gilles’s answer, (prog1; echo $? > /tmp/prog1.status) | prog2 is an approach.  prog2 could either read standard input to the end, and then read /tmp/prog1.status, or check for the existence of /tmp/prog1.status periodically while reading the standard input.


0

At the risk of beating a dead horse, the misconception seems to be that A | B is equivalent to A > temporary_file B < temporary_file rm temporary_file But, back when Unix was created and children rode dinosaurs to school, disks were very small, and it was common for a rather benign command to consume all the free space in a file ...


0

A single while loop would do here, I think. I think it would make no difference, really, since you're calling executables every iteration anyway. In this way you can share the global variable you had hoped for. Like: source_cmd | while read var do fn1 "$var" | fn2 "$var" done But I think better still would be to change the workflow a ...


1

ifne doesn't set an exit code based on whether the input is empty or not, so && and || aren't going to work as hoped. An alternate approach to Babyy's answer is to use pee from the same package: printf "asdf\n" | pee 'ifne cat -' 'ifne echo "stream not empty"' This works like tee, but duplicates the input stream into a number of pipes, treating ...


4

use this format: printf "" | ifne bash -c "cat - ; echo 'stream not empty' " output is none, and printf "bb\n" | ifne bash -c "cat - ; echo 'stream not empty' " output is: bb stream not empty


0

This is basically a negative answer. It appears that neither dd, nor mbuffer, nor even pv works is all cases, in particular if the rate of data generated by the producer can vary a lot. I give some testcases below. After typing the command, wait for about 10 seconds, then type > (to go to the end of the data, i.e. wait for the end of the input). zsh -c ...


2

There are no "rules" as such. Some programs take input from STDIN, and some do not. If a program can take input from STDIN, it can be piped to, if not, it can't. You can normally tell whether a program will take input or not by thinking about what it does. If the program's job is to somehow manipulate the contents of a file (e.g. grep, sed, awk etc.), it ...


8

This is an interesting question, and it deals with a part of the Unix/Linux philosophy. So, what is the difference between programs like grep, sed, sort on the one hand and kill, rm, ls on the other hand? I see two aspects. The filter aspect The first kind of programs is also called filters. They take an input, either from a file or from STDIN, modify ...


12

There are two common ways to provide inputs to programs: provide data to STDIN of the processes specify command line arguments kill uses only command line arguments. It does not read from STDIN. Programs like grep and awk read from STDIN (if no filenames are given as command line arguments) and process the data according to their command line arguments ...


0

You can use a while loop: while IFS= read -r _file do cp "$_file" ~/Desktop/temp done < "files.txt" IFS is set to null causes no splitting is performed, and -r tell bash escapes backslash character.


1

xargs does an unfortunate amount of parsing on its input, and depending on what characters occur in filenames (spaces, quotes/apostrophes, backslashes, tabs, etc) it can mangle them in a number of ways. The best way to handle filenames is as a null-delimited list and using xargs -0 (which turns off all of the parsing). If the file list were generated from ...


1

I found a solution in which I needed to replace a space with a backslash and a space on OS X. Here is what I came up with: echo "Hello World" | sed 's/ /\\ /g' | xargs echo With this all the spaces are retained.



Top 50 recent answers are included