New answers tagged

3

Assuming a shell like bash or ksh: max_time=3 SECONDS=0 IFS=$'\n:' grep -v '^#' ref.txt | while read tag cmd; do if (( SECONDS < max_time )); then echo "starting '$cmd' (tag is '$tag')" sleep 2 # for simulation purposes else echo "did not have time to start '$cmd' (tag is '$tag')" fi done This will give the ...


1

logic to display the processes which haven't started after 180 secs. Assuming the shell is a bourne shell. I testing on bash and zsh # ... # generate a timestamp in seconds since UNIX epoch START_TIME=$(date '+%s') until [[ $(date +%s) -gt $((START_TIME + 180)) ]] do # greps for the processes running echo " N number of processes are running" #&...


2

Try using fgrep (or the -F option to grep that does the same), and write your query without escaping the "<" and ">". I'd also suggest using single-quotes ' rather than double-quotes ", since the shell may expand what it think is variables and such when you use double-quotes. fgrep -i 'template <int N>' *


7

Grep interprets \n as a newline character. It looks like your file does not have newline characters, it has \ followed by n. To search for literal backslashes, you must double them: $ grep -o '\\n[^\\]*\\n' o.txt \n29\n \n3 days\n \n59\n \n7 days\n \n99\n \n12 days\n With GNU grep, the output can be easily cleaned up to remove the \n: $ grep -oP '(?<...


2

With zsh: print -rl ./**/results.out(.e_'grep -q string $REPLY'_:h) this searches recursively for regular files (.) named results.out, runs grep -q ... on each of them and if that evaluates true it prints only the head of the path (the path without the last element). Another way with find and sh, using parameter expansion to extract the head: find . -...


1

Here you are: ifconfig -a | grep -e "inet [0-9]" | cut -d" " -f 2 Most of the given answers won't work well on Mac OS X! The easiest thing you can do, is using cut or awk.


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' -...


1

for i in $(find . -type f -name "results.out); do grep -l "string1" $i ; exitcode=${?} if [ ${exitcode} -eq 0 ] # string1 is found in file $i then path=${i%/*} echo ${path} fi done


0

Assuming I understood correctly you want to do just that: find . -type f -name "results.out" -exec grep -l "string1" {} \; | xargs dirname First part gets matching filenames, then xargs passes those as an argument to dirname program which 'strips' filelame from path


0

Answer intended for original version of question For your one command, replacing the grep-tail-grep-if commands, try: awk '/status change/{last=$0} END{printf "It is %sBackup\n",(last~/Active/?"":"not ")}' "/apps/tests/$2" How it works: /status change/{last=$0} Every time that a line containing status change is found, it is saved in the variable last. ...


2

The following sh/Bash one liner is another method, though will only work in the current directory, and doesn't recurse: for f in ./*.txt; do if grep -l 'LINUX/UNIX' "$f"; then cp "$f" /path/to/dest/; fi; done The -l option to grep will print a list of the files which are being copied, though you could use -q if you don't want to see anything on the screen....


12

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


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 "$@"...


0

After a little while I came back to this question, and just want to show my fixed command which did the trick, for the record. tr -s '[:blank:]' '[\n*]' < filename.txt | grep -wo '[[:lower:]]*' | sort -u | less -N


1

starting my answer based on this answer: 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" a 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. and converting the answer to full length IPv6, etc...: fgrep -oE "\b([...


2

On Linux the ps command detects whether the output is to a terminal or not. If it is to a terminal then it truncates the output to the terminal width. If it is not on a terminal then it doesn't truncate. Effectively it behaves as if it automatically adds the ww flags. You can see the difference by typing ps aux and ps aux | cat In your example you are ...


2

If your file is called e.g ips you can write somethinng like: while read -r ip do if [[ $ip == "$1" ]]; then shift printf '%s\n' 'action to take if match found' else printf '%s\n' 'action to take if match not found' fi done < ips Then you can pass the parameters as follow the the ...


1

Redirect that output to some outputFile Simply grep it with pattern as, grep -sE "159.143.23.12|134.12.178.131|124.143.12.132" <outputFile>


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.


3

You are using non standard BSD options, whether under Linux or Solaris. The portable way is to use the -o option to specify which fields you want to appear, here for example ps -o pid,args. Note that unless you are using a recent 11.3 update or newer, the argument list will be limited to 80 characters unless you are root or own the process. Alternatively, ...


0

With POSIX awk: awk '{sub(/\r/,"")}1' CRLF.txt > LF.txt awk '{sub(/$/,"\r")}1' LF.txt > CRLF.txt


0

The question nearly gives it away, if you're familiar with pipes: To do this, note the who command will show each user who is logged in along with identification of their computer. You can feed this information to the grep command, to only display strings that contain the "kwantlen.ca". You can then further feed this output to the wc command, ...


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-...


1

GNU grep is able to do this quite simply. From man grep: Two regular expressions may be joined by the infix operator |; the resulting regular expression matches any string matching either subexpression. grep "ERROR\|FAIL\|WARN" /path/to/example.log egrep eliminates the need for escaping the | symbols. egrep "ERROR|FAIL|WARN" /path/to/example.log


1

Now, that your Data format has been established, the answer becomes a lot simpler: grep was built for this. Use as grep '<PATTERN>' <dataFile> Where <PATTERN> is SearchWORD1 or SearchW1\|SearchW2 The answer below was written, when me and @murphy still had wrong assumptions about the dataformat: Here is a one-line awk program that only ...


1

I suppose your log file looks like this? example.log: [09:44:22] [main] ERROR [url/location] - A ONE LINE ERROR [09:44:22] [main] ERROR [url/location] - A MULTI LINE ERROR with whitepace indention [09:44:22] [main] ERROR [url/location] - A MULTI LINE ERROR with tab indention [09:44:22] [main] SOMETHING DIFFERENT [09:44:22] [main] SOMETHING ...


8

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


1

Use ip addr show with -o flag. For instance, here's all IPv4 addresses of my connected interfaces $ ip -4 -o addr show | awk '{print $4}' 127.0.0.1/8 10.42.0.1/24 192.168.0.78/24 10.0.3.1/24 Getting only specific addresses that start with 10. like you have can be done this ...


1

To get all inet IP: ifconfig -a | grep -oP 'inet \K\S+' In order to get just 10.16 family: ifconfig -a | grep -oP 'inet \K10\.16\S+'


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 } } ...


2

With awk: awk '{if($1~/^Query_/){c=0;delete a;a[0]=$0}else{c++} if(c<5){a[c]=$0} if(c==5){for(i in a){print a[i]}} if(c>5){print}}' file In the first line the first field $1 is checked whether it begins with Query_. If so, the counter variable c is set to 0. The array a is removed, and the first element of the array is set to the ...


-1

ack is a default package installed on Debian based distributions. If you see a message like this when you run ack -h then you need to use ack-grep or follow the step at http://beyondgrep.com/install/ on Renaming ack-grep on Debian-derived distros Try hg log <filename> | ack-grep changeset to see if ack-grep is installed properly.


0

Solution: List files in target dir Replace to wildcard match with sed Pipe to rsync --exclude-from find target_dir | sed -r 's/\.\/(.+?)-.*/\1*/' | rsync --verbose --ignore-existing --exclude-from - src_dir/* target_dir


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

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 ...


1

Analyzing ps aux | grep -v grep | grep firefox ps aux will give you the output of processes. The first grep (grep -v grep) will remove any line that will be feeded in it from ps aux that will contain the word grep. After the output will be grepped again (grep firefox) for the word firefox and give you the output.


1

The grep in ps | grep -v grep removes any grep processes from the listing, otherwise the grep looking for firefox would also match the grep firefox command line. Try it: a simple ps uax | grep whatever will likely show a single grep.


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 ...


0

You could try something like this: awk -F'\t' -f script.awk column.names data where script.awk is: NR == FNR { columns[NR] = $0; next } NR > FNR && FNR == 1 { for (i = 1; i <= NF; i++) { for (j = 1; j < NR; j++) { if ($i ~ columns[j]) { selected[i] = 1; break; } ...


0

I suppose OP can do the follows: Read header data.txt and convert columns into rows Grep the rows to obtain numbers to match list.txt Pass data.txt through the cut If columns are tab delimited the script can be: cut -f 1,$( head -n1 data.txt | tr '\t' '\n' | grep -nf list.txt | sed ':a;$!N;s/:[^\n]*\n/,/;ta;s/:.*//' ) data.txt


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


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 ...


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 ...


0

You could use a perl-command like perl -ne '$_=~/inet\s(\d+\.\d+\.\d+\.\d+)\s/; print $1,"\n"' This uses a regex to find the IP after inet and prints it. Just pipe your output through it. Example: ifconfig -a | grep 10.16 | perl -ne '$_=~/inet\s(\d+\.\d+\.\d+\.\d+)\s/; print $1,"\n"'


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


2

To see how ls behaves when its output is being redirected, you can try running ls | cat or ls -1 which is how ls behaves when its output doesn't go to a TTY.


0

To parse XML, use an XML parser. XMLStarlet is a command line XML parser that is very good for this sort of situation. Assuming that your XML is complete (it's missing </Job></Data> at the end as it is written now) then you can extract the value of the Output_Path node with $ xml --template --value-of '//Output_Path' -nl input.xml xxxxxxxx.xx....


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 ...


0

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off: function grep1() ( nlines=$(wc -l < "$1") nlw=$(printf "%d" "$nlines" | wc -c) nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 ...


0

Luis, your last edit explains it all. It’s not your code that’s broken; it’s the metadata for certain specific jpeg files. I’m not sure how to fix that but at least I can tell you that you need to look for the answer in the jpeg files themselves. I thing Gimp and ImageMagick will let you fix those kind of things but you would have to read the manuals in ...



Top 50 recent answers are included