Tag Info

Hot answers tagged

29

tac/grep Solution tac file | grep whatever Or a bit more effective: grep whatever < <(tac file) Time with a 500MB file: real 0m1.225s user 0m1.164s sys 0m0.516s sed/grep Solution: sed '1!G;h;$!d' | grep whatever Time with a 500MB file: Aborted after 10+ minutes. awk/grep Solution: awk '{x[NR]=$0}END{while (NR) print x[NR--]}' ...


27

Sort of. The shell has no idea what the commands you are running will do, it just connects the output of one to the input of the other. If grep finds more than 10 lines that say "hello world" then head will have all 10 lines it wants, and close the pipe. This will cause grep to be killed with a SIGPIPE, so it does not need to continue scanning a very ...


14

This solution might help: tac file_name | grep -e expression


14

Here are some points you could start with: Have a look at the packages installed on your system with pacman -Q and remove the ones you don't need. A good start may be to append the -t switch: Restrict or filter output to packages not required by any currently installed package. Clean the package cache of pacman with pacman -Sc Always use pacman ...


12

When a program tries to write to a pipe and there is no process reading from that pipe, then the writer program receives a SIGPIPE signal. The default action when a program receives SIGPIPE is to terminate the program. A program can choose to ignore the SIGPIPE signal, in which case the write returns an error (EPIPE). In your example, here's a timeline of ...


9

The construction <(tac file) causes to shell to: Create a pipe with a name On systems such as Linux and SysV which have /dev/fd, a regular pipe is used, and /dev/fd/<the-file-descriptor-of-the-pipe> is used as the name. On other systems, a named pipe is used, which requires creating an actual file entry on disk. Launch the command tac file and ...


8

Sure. bash someScript.sh | sed -n '2 p' will filter your output and just print the second line of it. To make that a parameter to vim: vim "$(bash someScript.sh | sed -n '2 p')"


7

If the file is really big, can not fit in memory, I will use Perl with File::ReadBackwards module from CPAN: $ cat reverse-grep.pl #!/usr/bin/perl use strict; use warnings; use File::ReadBackwards; ...


7

This one exits as soon as it finds the first match: tac hugeproduction.log | grep -m1 WhatImLookingFor The following gives the 5 lines before and after the first two matches: tac hugeproduction.log | grep -m2 -A 5 -B 5 WhatImLookingFor Remember not to use -i (case insensitive) unless you have to as that will slow down the grep. If you know the exact ...


4

What is difference between | and <<()? There is a difference between them: | cause each command run in a separated subshell. <() run the command, which is substituted in background. For the next two question, we will do some strace: pipe: $ strace -fc bash -c 'tac /usr/share/dict/american-english | grep qwerty' $ time seconds ...


3

The programs often call the underlying program libraries you refer to. The commandline is there to chain these tools together which is much more efficient (in development time) then writing and compiling a program in C that calls the libraries. While only being marginally slower in execution time. AFAIK this usage of small programs together was, and is, the ...


3

Following KISS principle, I'd just monitor free memory with free -m trying to run the same set of user apps for the same period of time. Just keep in mind, that 'cached' is actually free memory as well, so you need the 2nd line which shows what real used and free memory volumes are. Regarding CPU — WMs usually don't hog CPU by themselves — they do that ...


3

Sortof, the pipeline works like this: it first executes the first command and then the second command in your case. That is, let's have A|B be the command given. Then it is uncertain whether A or B starts first. They might start at exactly the same time if there are multiple CPUs. A pipe can hold an undefined but finite amount of data. If B tries to read ...


3

The client-server capabilities of vim depend on X11, citing from its help: The communication between client and server goes through the X server. The display of the Vim server must be specified. The usual protection of the X server is used, you must be able to open a window on the X server for the communication to work. [...] A non ...


2

When you read() an inotify fd, the name field of the returned struct tells you which file was modified relative to the directory being watched, so you shouldn't have to stat every file in a directory after the event. See http://linux.die.net/man/7/inotify Specifically: struct inotify_event { int wd; /* Watch descriptor */ uint32_t ...


2

If being short to type is paramount and the file isn't very large: nth_line=$(sed -n ${n}p) nth_line=$(sed \!${n}d) If the file is long and you're only interested in one line, use sed to print the desired line and quit, or tail to remove the previous lines and head to extract the first line of the result. nth_line=$(sed -n -e "$n {" -e p -e q -e "}") ...


2

You can use the ondemand cpu-freq governor, as long as you set the ignore_nice_load parameter to 1. From Documentation/cpu-freq/governors.txt, ondemand section: ignore_nice_load: this parameter takes a value of '0' or '1'. When set to '0' (its default), all processes are counted towards the 'cpu utilisation' value. When set to '1', the processes ...


2

As you (incorrectly – what you used is a shell pattern) mentioned it in the subject, you should use regular expressions: find . -iregex '.*\.[ch]+' The above is lazy approach, which will also find .ch, .hh and alike, if there exists. For exact matches you still have to enumerate what you want, but that is still easier with regular expressions: find . ...


1

This is as efficient as it gets. You can see what system calls the shell makes by looking with strace (or the equivalent on unix variants other than Linux). strace bash -c '[[ -d $directory_path && -r $directory_path && -x $directory_path ]]' … stat64("foo", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 faccessat(AT_FDCWD, "foo", R_OK) ...


1

Portably/standardly (POSIX, Unix (SUS) and Linux (LSB) standards) and efficiently, you'd write it: find . \( -name '*.cc' -o -name '*.CC' -o -name '*.[cChH]' \) \ -type f -exec grep -n -- "$1" /dev/null {} + The most important point here is to use + instead of ;. Otherwise, you'll run one grep command per file. The -H option is GNU specific, but adding ...


1

Can be shortened to this single line: find -type f -regextype posix-egrep -iregex '.*\.(cc|h|c)$' -exec grep -nHr "$1" {} \;


1

grep has no direct control of the pipe (it is just receiving data), and the pipe has no direct control of grep (it is just sending data)... What grep, or any other program does, is entirely up to that programs internal logic. If you tell grep via command line options to make an early exit-when-found, then it will, otherwise it will chug on to very end of ...


1

For each pattern, you're invoking a new instance of the sqlite program which connects to the database anew. That's a waste. You should build a single query that looks for any of the keys, then execute that one query. Database clients are good at executing large queries. If the matching lines in the keys file only contain digits, then you can build the query ...


1

First things first, you really replace the if with a list. Actually I would even replace the [[]]s with []s, and then run in dash or other lighter sh. This even seems simple enough to ditch the entire for, and run with xargs (always my preference, better performance) So for example, maybe something like this ... grep ^[0-9] keys | xargs -P0 -I '{id}' \ sh ...


1

Use head and tail, e.g. accessing the 2nd line of an output: bash someScript.sh | head -2 | tail -1


1

I would suggest AUTOMATING looking at your own very old files and see if its necessary and remove them. This can done with two things. One writing a find command to look for very old files. and using this output (maybe pipe) to lograte script to archive files older than says 3 months and delete files older than 3 months in archive and send a mail to 1 day ...



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