Hot answers tagged

19

Try: sed -i.bak '/firmware_revision/ s/test/production/' myfile.py Here, /firmware_revision/ acts as a condition. It is true for lines that match the regex firmware_revision and false for other lines. If the condition is true, then the command which follows is executed. In this case, that command is a substitute command that replaces the first ...


15

Use the \ character to escape the * to make it a normal character. grep '^\*\*' test.out Also note the single quote ' and not double quote " to prevent the shell expanding things


13

The shell stores the exit value of the last executed process in the variable ?. You can assign its value to one of your own variables like this: check=$? If you want to act on this value, you may either use your check variable: if (( check )); then # do things else # do other things fi or you could skip using a separate variable and having to ...


13

Try: grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest Because this command uses NUL-separation, it is safe for all file names including those with difficult names that include blanks, tabs, or even newlines. The above requires GNU cp. For BSD/OSX, try: grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0 sh -c 'cp "$@"...


12

awk 'FNR == 1 { f1=f2=f3=0; }; /one/ { f1++ }; /two/ { f2++ }; /three/ { f3++ }; f1 && f2 && f3 { print FILENAME; nextfile; }' * If you want to automatically handle gzipped files, either run this in a loop with zcat (slow and inefficient because you'll be forking awk many times in a loop, once ...


12

More portably (POSIX features only): find . -type f -name '*.txt' -exec grep -q LINUX/UNIX \; -exec cp {} /path/to/dest \;


11

Set record separator to . so that awk will treat whole file as a one line: awk -v RS='.' '/one/&&/two/&&/three/{print FILENAME}' * Similarly with perl: perl -ln00e '/one/&&/two/&&/three/ && print $ARGV' *


10

RH[A-ZA-Z] is a regular expression that includes a single character class that repeats the same set of characters twice. It matches RH followed by any character from A to Z. It places no restrictions on the fourth character, it doesn't even require there to be a fourth character. Obviously, that doesn't do what you want. Try this: RH[A-Z][A-Z] or (with ...


8

Take help from sort and uniq: grep -iEo "(red|white|blue)" somefile.txt | sort | uniq -c sort sorts the output uniq -c gets the frequency


8

awk -v keyword=keyword -F/ '/\[GET\]/ { id=$NF; next } $0 ~ keyword { print $0 > id }' log


8

grep's -o will only output the matches, ignoring lines; wc can count them: grep -io "nicolas bomber" annuaire | wc -l or simply, grep -ioc "nicolas bomber" annuaire As you commented, you can match any number of whitespaces coming in between the words, using -z option, grep -iz "nicolas[[:space:]]*bomber" annuaire | wc -l From man grep -i, --ignore-...


7

As you wanted to check the line which starts with ** and ends with ), you can combine two grep operation like this, grep '^*\*' test.out | grep ')$' Or with single grep command like this, grep -E '^\*\*.*\)$' test.out Explanation ^\*\* : match line which starts with ** .* : match everything after ** \)$ : match line which also has ) at the end of ...


7

Well, if you know your IP starts with 10.16, it's trivial: ifconfig -a | grep -oP '\b10\.16\.[0-9.]+\b' Or, if your grep doesn't support -P or -o: ifconfig -a | awk '/10\.16\./ && /inet/{print $2}' If not, you could find all lines starting with inet and print their second field: ifconfig -a | awk '$1=="inet"{print $2}' That, however, would ...


5

Although it's an unconventional application of grep, you can do it using grep -m1 "" file.txt It works because the empty expression matches anything, while -m1 causes grep to exit after the first match -m NUM, --max-count=NUM Stop reading a file after NUM matching lines.


4

You can do it without the grep: df --output=target,size /mnt/xyz | awk ' NR==2 { print $2 } ' df accepts as argument the mount point; you can tell to awk too to print both the second line only (NR==2) , and the 2nd argument, $2. Or better yet, cut the target as you are not outputting it, and it becomes: df --output=size /mnt/xyz | awk ' NR==2 ' When I ...


4

As per the comments, there are various reasons why this may not match, but look "invisible". One common failure mode is the file is in DOS format. In your case, you have hidden whitespace (space or TAB characters) at the end of the line. Commands such as tr ' ' ! < filename or cat -e filename can help expose these characters.


4

If you want to count the number of times 1 is in the second field before/between each line that says Transition you could use awk like awk '$2 == "1" {count++;} /Transition/ {t_count++; print t_count "\t" count; count=0;}' <input file> which will increment a counter each time 1 is in field 2, and every time the line matches Transition will print the ...


4

Your bottleneck is reading the recon.txt file line by line in the shell. To get the failing lines you could preprocess the lines in the logs to look like the lines in recon.txt, then use comm(1) to find the set difference, perhaps like this: comm -23 \ <(sort -u recon.txt) \ <(sed 's/.*| id=\([0-9]*\)| version=\([0-9]*\)|.*/\1,\2/' worker-...


4

On older machines with old-school sed that doesn't support option -i: TF=$( mktemp -t "${0##*/}"_$$_XXXXXXXX ) && \ trap 'rm -f "$TF"' EXIT HUP INT QUIT TERM && \ sed '/firmware_revision/ s/test/production/' myfile.py >"$TF" && \ mv -f "$TF" myfile.py


4

Yes , You have lot of options/tools to use. I just tried this , it works: ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" so you can use grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" to grep the ip addresses from your output.


4

If you have GNU find, you can print the path using the %h format specifier %h Leading directories of file's name (all but the last ele‐ ment). If the file name contains no slashes (since it is in the current directory) the %h specifier expands to ".". So for example you could do find . -name 'results.out' -...


3

You may also $ zcat test.gz.gz | zgrep "whatever" zcat and zgrep works like cat and grep but on compressed data streams.


3

For compressed files, you could loop over each file and decompress first. Then, with a slightly modified version of the other answers, you can do: for f in *; do zcat -f "$f" | perl -ln00e '/one/&&/two/&&/three/ && exit(0); }{ exit(1)' && printf '%s\n' "$f" done The Perl script will exit with 0 status (success)...


3

It's not the shell None of the answers so far has touched on the real problem. It would be helpful to explain why it does not work as you expect. grep -i "^**" test.out Because you have quoted the pattern to grep, * is not expanded by the shell. It is passed to grep as-is. This is explained in the manual page[1] for bash[2]: Enclosing characters in ...


3

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does. ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. From it we got grep, which can be seen as a shortcut or alias for a specific ...


3

It's possible for a command to detect when its output is going to a TTY or not. Thus in this particular case, when ls detects that its output is not going to a TTY, it behaves as if -1 were passed as an argument. You can see this, and that grep is not doing anything special by using cat: ls | cat


3

There must be something simpler than this: awk ' /^ *UPDATE/ { prevsep=";" } /^ *(SET|WHERE)/{ prevsep="" } { if(previous)print previous prevsep; previous = $0; prevsep = sep } /^ *(UPDATE|SET|WHERE)/{ prevsep="" } /^ *SET/ { sep="," } /^ *WHERE/{ sep=" and" } END { print previous ";" }' Each line is remembered in previous and printed with a previous ...


3

The ps command will output all your currently running processes. The first grep will remove the grep process from this list. The second will extract any firefox process in the filtered list. This is probably a partial attempt to get the process ID (PID) of the firefox process, possibly just to see if it's running, or to terminate it. If this is the case, I ...


3

When you do a command such as ps aux | grep firefox Then the grep process itself may show in the output because the word you are looking for is present. e.g. on my machine I run chrome and the similar results: % ps aux | grep chrome sweh 3384 0.0 0.0 11128 1024 pts/1 S+ 07:08 0:00 grep chrome sweh 23698 0.0 0.0 6384 620 ? ...


3

With sed: sed ' /^Query_/{ #starts loop when meet patten :a $!{ N /\nQuery_/!ba #untill meet next pattern } /\(\n.*\)\{6,\}/{ #checks how many lines in block $b #for end of file s/\nQuery_/\n&/ #marks lines to print } } ...



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