Tag Info

New answers tagged

-1

Always use the simplest tool for the job. As you want to operate on the current directory only, this command will work fine: rm -i *.swp


0

If you want to be more restricted, you can use this: find . -type f -name "*.swp" -exec rm -f {} \;


4

Or a variation with find alone e.g.: find . -name "*.swp" -ok rm {} + or just without confirmation (WARNING!): find . -name "*.swp" -delete


5

The error message you received probably indicates that no file matched the name pattern .swp$. A generally safer way to do what you wrote (because it will handle any file name): find . -name '*.swp' -print0 | xargs -0 rm -i --


5

Your grep doesn't support the -A flag, so you can't use that. But you should be able to get the same result with awk. awk -v dt=$(date +%m/%d) '$0~dt{counter=5}counter>=0{print;counter--}' file.txt This sets a counter to 5 when a match is found, and prints and decrements the counter while it's not negative.


1

Here’s a way to get the results you want with GNU awk: awk ' BEGINFILE { if (FILENAME ~ "^animals/" || FILENAME ~ "/animals/") { this_name = substr(FILENAME, index(FILENAME,"animals/")+length("animals/")) i = index(this_name, ".") if (i > 0) this_name = substr(this_name, 1, i-1) ...


0

I think if you give the patterns as input to grep using stdin, it will concatenate results for all patterns. So AFAIK the only way to overcome this problem is to call a new instance of grep for each pattern. This script would work in this case for animalName in $(ls -1 ~/animals | cut -d. -f1); do echo "$animalName: $(grep -R -h -c $animalName ...


0

sed -E ' s/[0-9.+-]*e[-+]?[0-9]{2}/ &/g s/ *(.{22}[0-9])/\1/g' < input.txt > output.txt That assumes GNU or FreeBSD (or derivative like OS/X) sed, or a sed conformant to the next version of the Unix/POSIX standard (for -E).


1

There are at least two ways of speeding up grep CPU-wise: If you're searching for a fixed string rather than a regular expression, specify the -F flag; If your pattern is ASCII-only, use an 8-bit locale instead of UTF-8, e.g. LC_ALL=C grep .... These won't help though if your hard drive is the bottleneck; in that case probably parallelizing won't help ...


9

GNU grep's -w will only consider the 26+26+10+1 (ASCII letters, digits and underscore) as word constituents. You can't change that, even by using a different locale (even in a locale where é is considered a letter, Stéphane would still be St and phane separated by the non-word é). You can however implement that logic by hand: grep -E ...


0

The question is not clear on whether or not the 9999999999 is always present, or whether there can be more than one such instance in the input file. So here is a sed version which caters for all of these situations. See man sed (option -i) for in-place update of the input file. sed -n '/9999999999/{H;b n}; p; :n; ${g;s/\n//p}' file


1

Other sed solution sed -i ' /9999999999/{h;d}; # move match string in hold space $G; # append string from hold space to end s/\n$// # avoid empty line if pattern have not met ' BADINS0000001065_0000000000*


1

With awk you can do: awk ' /PATTERN/ { save = $0 ; next } { print } END { print save } ' infile > outfile where you have to replace PATTERN by the actual pattern and infile is your data file; rename those appropriately. The code works as follows: /PATTERN/ { save = $0 ; next } - if the pattern is found save this line for later use and skip ...


22

The point of grepping output that is thrown away is that the writer only wants the return status of grep. He/She only wants to know whether a pattern matched or not. In your case, the last grep checks if the earlier command's output contains any lines start with 200. In modern POSIX system, you can do it all with grep -q without redirecting to /dev/null: ...


23

Your suspicion is correct; the exit status of the last command of the script will be passed to the calling environment. So the answer is that this script will return an exit status 0 if grep matched someting in the data, exist status 1 if there was no match, and exit status 2 if some error occurred.


1

Single quotes do not need to be escaped when they are inside double quotes: grep "mobNo='[^']*'" file Also, grep uses regular expressions, not shell patterns (globs). In the shell, * matches zero or more of any character at all. In a regex, however, * matches zero or more of the preceding character. Here we use [^']* which means zero of more of any ...


0

If the problem is not I/O bound you could use a tool that is optimized for multi-core processing. You might want to take a look at sift (http://sift-tool.org, disclaimer: I am the author of this tool) or the silver searcher (https://github.com/ggreer/the_silver_searcher). the silver searcher has a file size limit of 2GB if you use a regex pattern and not a ...


1

Head piped to grep gets you halfway there. head -50 filename | grep string


2

In loop way GNU sed can do task too : for f in *.sql do sed -e '1 F' -e '51 Q' -e '/pattern/! d' "$f" done


8

awk (assuming your implementation supports the nextfile statement) can do this quite nicely: awk 'FNR > 50 { nextfile }; /foobar/ { print FILENAME ": " $0 }' ./*.sql The first statement skips to the next file once 50 records have been processed. The second statement prints the filename and the matching line for any line containing foobar. If your awk ...


4

This solution works, but I feel that it is clumsy. Searching the first 50 lines for the string "foobar": $ for I in *.sql ; do echo $I && head -n 50 $I | grep foobar ; done


4

There are two easy solutions for this. Basically, using xargs or parallel. xargs Approach: You can use xargs with find as follows: find . -type f -print0 | xargs -0 -P number_of_processes grep mypattern > output Where you will replace number_of_processes by the maximum number of processes you want to be launched. However, this is not guaranteed to ...


0

cd to your folder containing your pdf-file and then.. pdfgrep 'pattern' your.pdf or if you want to search in more than just one pdf-file (e.g. in all pdf-files in your folder) pdfgrep 'pattern' `ls *.pdf` or pdfgrep 'pattern' $(ls *.pdf)


2

Based on the input you show in your question, this should work: $ grep -oP '^[ @]*R.* \K.*' gitolite-info-output SecureBrowse anu-wsd entrans git-notes gitolite gitolite-admin indic_web_input proxy testing vic This is using GNU grep's -P switch to enable Perl Compatible Regular Expressions which give us \K : "Exclude anything matched up to this point". ...


1

Using (gnu)grep: grep -m1 -oP '(?<=class=val>).*?(?=</td>)' grep -m1 -oP 'class=val>\s*\K[0-9.]*' # \cite{Costas)


2

There is no need to mix many instruments. Task can be done by sed only sed '/^INFO\|^DEBUG\|^TRACE\|^ERROR/{ /Logger2/{ :1 N /\nINFO\|\nDEBUG\|\nTRACE\|\nERROR/!s/\n// $!t1 D } }' log.entry


1

Based on one answer at http://stackoverflow.com/questions/9605232/merge-two-lines-into-one this seems to fit the bill #!/usr/local/bin/bash PATTERN1='TRACE *'; PATTERN2='DEBUG *'; PATTERN3='INFO *'; PATTERN4='ERROR *'; LINEOUT="" while read line; do case $line in $PATTERN1) echo $LINEOUT LINEOUT="$line" ...


1

perl filter for multiline log records (record begin mark) Use the following perl script as a working prototype. Usage script_path regular_expression log_files e.g. script_path "line \d" log_file_1 log_file_2 #!/usr/bin/perl $pattern = qr/(?^s)$ARGV[0]/; shift; # process filtering expression # (?^s) - treats matched string as single line my $line = ''; # ...


2

If you want to stick with you one, use this: grep -E "^[[:alpha:]]+::[[:alpha:]]+\(\)[[:space:]]\[.*\]$" For example: $ echo "Abc::xyz() [18-Feb-15 12:09:16]" | \ grep -E "^[[:alpha:]]+::[[:alpha:]]+\(\)[[:space:]]\[.*\]$" Output: Abc::xyz() [18-Feb-15 12:09:16] This can be made simpler: grep -E "^[^:]+::[^(]+\(\) \[[^]]+\]$" Check: $ echo ...


-1

try entering awk '/changeset/ {print}'


0

Saw this on the ack documentation page, maybe it would help? --[no]filter Force ack to treat standard input as a pipe (--filter) or tty (--nofilter)


1

Use all patterns inside a group: grep -A 25 -E '(show\(authenticated = |onAllPanelsCollapsed\(\)|makeExpandedInvisible)$' As we have grouped the patterns using (), they will be treated as a whole and this will match any one of the three patterns at the end of the line. Previously, in your command grep -A 25 -E 'show\(authenticated = ...


0

you can use find without ‍‍-exec‍ as well: mkdir /path/to/dest cp `find /path/to/source -name "*.jar"` /path/to/dest


1

It's possible some kind of character set problem is throwing things off for you. Here are some commands you should be able to use to re-create a successful usage of grep that may help you. First let's make a text file in the terminal to avoid any character set problems: echo -e "Hello\nHallo" > foo.txt The -e flag tells echo we want the \n to be ...


1

in perl regexp (grep -P ...) you may use \Q...\E to protect meta chars grep -P "(^|,)\Q$EMAIL\E(,|$)" file.csv where: (^|,) = start of field (,|$) = end of field


1

There are some mistakes in your script: Missing space in [-z $word2] Missing double quote in variables Useless use of more | grep Parsing output of ls is broken grep will fail if $word1 or $word2 start with dash - Here's a fix: word1=$1 word2=$2 for f in ./*.txt; do printf '>%s\n' "$f" ###if word2 is empty; then execute if [ -z "$word2" ]; ...


6

Try this: touch -d"April 13 3 AM" file1 touch -d"April 13 9 AM" file2 find . -newer file1 ! -newer file2 -exec grep -l "pcV6URY" {} + rm file1 file2 How it works find can work directly with times but touch handles human-style dates better: touch -d"April 13 3 AM" file1; touch -d"April 13 9 AM" file2 This creates two files to mark the beginning and end ...


0

I solved this one for me using grep and -A option with another grep. grep first_line_word -A 1 testfile | grep second_line_word The -A 1 option prints 1 line after the found line. Of course it depends on your file and word combination. But for me it was the fastest and reliable solution.


2

If you don't mind using two different commands for the file names and content, the below commands will help you. find /sys -name "*filesystem*" The above command will find all the files/directories with "filesystem" as part of the filename/directoryname. grep -rn "filesystem" /sys/* The above command will look for all the files containing "filesystem" ...


2

It looks like gd is also an alias and that expands to -m and something. To illustrate: $ alias alg='alias | grep ' $ alias gd='-m foo' $ alg gd grep: invalid max count The error message you show is what grep prints when it is given the -m (max count) option and a non-numerical argument. If you run alias | grep gd, I bet you'll see it matches something ...


0

Remove the blank at the end of the alias definition (as suggested by rici) and your issue should be fixed. But; in such cases as yours, where you have not only synonyms or abbreviations in your alias but also functional code with pipes, it's better to define a function instead of an alias.


3

I think awk could help: awk 'BEGIN { sd = "20150408T13:29:28"; ed = "20150408T17:55:02"; } $1 "T" $2 >= sd && $1 "T" $2 <= ed' log


1

Certanly task can be done by sed sed '/20150408 13:29:28/,/20150408 17:55:02/! d' log_files but if lines did not have exact 20150408 13:29:28 script will print nothing and if its did not have exact 20150408 17:55:02 file will print every lines till the end. So the better is use date compare by script: limit1=$(date -d "20150408 13:29:28" +"%s") ...


1

Your shell is interpreting {1,3} as a brace expansion, resulting in grep seeing grep ^[0-9]1$ ^[0-9]3$ test123 as a result of which it takes ^[0-9]3$ to be an additional filename argument. You should always quote your regex to prevent such expansion by the shell i.e. egrep '^[0-9]{1,3}$' test123


1

Be aware that matching email addresses is a LOT harder that what you have. See an excerpt from the Mastering Regular Expressions book However, to answer your question, for a basic regular expression, your quantifiers need to be one of *, \+ or \{m,n\} (with the backslashes) pattern='^[a-zA-Z0-9]\+@[a-zA-Z0-9]\+\.[a-z]\{2,\}' grep "$pattern" regexfile ...


0

This oneliner gathers the mailq output (I often pipe that to a file and then run the script against the file so I can tweak it with less performance impact). Then it cuts out only the mail ID first by using the cut command to identify the proper field and then using egrep to clean that up (removing empty lines, irrelevant IDs ending in * and lines starting ...


2

If the order of the patterns is fixed then you can easily use grep as in: grep -E 'pattern1.*pattern2.*pattern3' But in case that all patterns must be present and they may appear in any order then you get combinatorical complexity; e.g. for two patterns: grep -E '(pattern1.*pattern2|pattern2.*pattern1)' (and for three patterns you'd have already eight ...


0

Yes. You can construct any regular expression you want and use it as the pattern in a grep command. It simply has to be a legal regular expression.


4

sounds like you're not using proper simple quote ('). Try to copy and paste this one: grep -E '( ^ | [[:space:]] )[A-Z]{2}[[:digit:]]{2}((- | [[:space:]] )[[:alnum:]]{4}) {3} ' (I have the same error in bash if I copy and paste your script, which use ’ instead of ')


1

What you're looking at is not a result, it's an error message. The format of grep's error when told to search through a non-existent file is grep: file name: No such file or directory. For example: $ grep foo bar grep: bar: No such file or directory That's why you get the grep: at the beginning and the No such file or directory at the end. That is also ...



Top 50 recent answers are included