Tag Info

New answers tagged

0

My files were encoded in iso-8859-1 so anything that tried to read the input in my default locale (utf-8) would not recognize the Japanese characters. In the end I managed to solve my problem with the following command: env LC_CTYPE=iso-8859-1 grep -nP '[\x80-\xff]' ./* -P is to allow for the Perllike syntax for character ranges. -n is for printing the ...


1

If you don't mind using perl, it has more extensive Unicode support in the form of classes such as {Katana} and {Hiragana} which I don't think are currently available in even in those versions of grep that provide some PCRE support. However it does appear to require explicit UTF-8 decoding e.g. perl -MEncode -ne 'print if decode("UTF-8",$_) =~ ...


0

Are you sure that it’s the “S” that’s causing the problem?  As I explained in my answer to your previous question, the “l” signifies that your operating system and filesystem support mandatory file locking, and mandatory file locking is enabled for this file.  I doubt that it’s a coincidence that both files you give as examples (in this question and the ...


0

Try this: grep '[^[:print:][:space:]]' (Depending on your locale setting maybe you have to prepend it by LANG=C.)


2

echo $above_string | grep -oP "^([^?]*\?){2}\K[^?]*" Change 2 to the n - 1 value in order to obtain the nth string. This assumes that you want the nth string in that line. You have n - 1 strings with no ? ending with a literal '?' (\? since it's a special character in perl regex). Then with \K you state you are not interested in the previous contents, ...


2

With sed you can do: sed '/\n/P;//d;s/[^?]*/\n&\n/[num];D' ...where you would replace the [num] above with some number representing the desired occurrence. If the numbered occurrence you specify does not exist, as is demonstrated in the following example, sed will simply print nothing at all. echo ,2,3 | sed '/\n/P;//d;s/[^,]*/\n&\n/4;D' Above ...


2

sed You can use sed for this, but it is not advisable, e.g. here is a zero-based solution that uses a quantifier to select the desired field: n=1 sed 's/\([^?]*? *\)\{'$n'\}//; s/?.*//' <<<"$above_string" Output: Elvis August 16


3

Using Awk to print the second and third records separated by newlines: awk -F"?" '{printf "%s\n%s\n", $2,$3}' Elvis August 16 Leonard Nimoy February 27 If you want to swap out the record, you can set it as a variable: awk -v record=2 -F"?" '{print $record}' Elvis August 16


8

How about using cut? If you'd like to print the 2nd pattern echo "$above_string" | cut -f2 -d "?" Second column onward echo "$above_string" | cut -f2- -d "?"


7

You have a file called a in your current directory. you haven't quoted the RE passed to grep, so this is what is happening: Given grep [aeiou] file1 The shell sees [aeiou] and because it's a valid (glob) pattern it tries to match that against a single letter file a, e, i, o, u. Since it succeeds it replaces the parameter with the file it matched, a. The ...


0

Here are a couple of solutions. Two-pass (without array): awk ' { if (NR == FNR) { if ($5 > max) max = $5 } else { if ($5 == max) print } }' textfile textfile NR is the record number (line number) counted across all the input.  FNR is the line number within the current file.  So, for ...


2

All you need is sort macadd | uniq -c as explained by @roaima but I just wanted to point out how you could do it using the same approach you had attempted. There's no reason to grep through the file, you can just feed it directly to while: while read mac; do echo "$mac"; done < macadd Also, grep has a -c option which counts matches. So, with a couple ...


1

Try this sort macadd | uniq -c


1

Sed solution: sed -n ':2;/IDA:6d87de8/{p;/exception/{:1;n;/IDA:/!{p;b1};b2}}' The script suppres output (-n option), until meet appropriate IDA (/IDA:6d87de8/) which will be printed (p). Then pattern(line) will be checked for "exeption" presence and, if so, starts to operate with next line (n). If the next line do not consist IDA: the line will be printed ...


1

This prints the respective data (each preceded with the line number) to files named as the IDA: awk 'match($0,/IDA:[^ ]+/) { print NR, $0 > substr($0,RSTART+4,RLENGTH-4) }' logfile


3

Perl solution: perl -ne '$id = "6d87de8e-1276-4496-b49d-dd4cd375cbe4"; print if $match = (/IDA:$id/ .. /IDA:(?!$id)/) and $match !~ /E0$/ ' *.log Explanation: /regex1/ .. /regex2/ returns true for lines between the matches. IDA:(?!$id) meand IDA: not followed by $id. the last line in a range is denoted by the E0 suffix ...


1

Inside comments (/* .*? */) remove everything except newlines; grep non empty lines: perl -p0E 's!(/\*.*?\*/)!$1 =~ s/.//gr!egs;' test.js |grep -nP '\S' which outputs: 9:(function(a,b){function d(b) {return!a(b).parents().andSelf().filter(f 17:(function(a,b){if(a.cleanData){var c=a.cleanData;a.cleanData=function(b


2

According to your data samples in your question this seems to be what you want (otherwise clarify your question, please): awk '$5 > max { max = $5 ; out = $0 } END { print out }' datafile This will print that line in datafile where the value in the 5th column is the maximum. The program works as follows: For each line the fifth column element will be ...


0

Using Awk: awk '/^*\/\(/ {gsub(/\*\/|\/\*!/,""); print NR":",$0}' js 9: (function(a,b){function d(b) {return!a(b).parents().andSelf().filter(f 17: (function(a,b){if(a.cleanData){var c=a.cleanData;a.cleanData=function(b


3

If there is only one foo3 in line sed -n '/foo3=/{s/.*foo3=//;s/\S*=.*//;p}' file.txt Suppress printing any line (-n options) exept which pushed by p. For lines which consists foo3=: Exchange everything before foo3= with it included (.*foo3=) to nothing (//). Remove everything which starts with some(*) non-space (\S) symbols with =. Prints resedue ...


2

sed '/^foo3=/P;/\n/!s/[^ ]\{1,\}=/\n&/g;D' <infile >outfile You may have to use a literal newline in place of the n above, but this will print only the contents between foo3 and foo4. For faster processing, get more explicit about it: sed '/\n/s/ [^ ]*=.*//p;/\n/!s/foo3=/\n\n&/;D' | grep . Or with an extra grep the top can be much faster ...


1

Try this: $ grep -Po "(?<=foo3\=).*(?=\s*foo4)" file.txt


1

OK, I think I’ve got it.  Your Source: (?<group>.*/).*\n regex is capturing, in the group group, everything after the Source:  up through the last / on the line.  So, for your example, it is capturing /disk/media/Camera/.  To capture the JPEG image filename, you want Source: .*/(?<group>.*)\n… OK, here we go again.  I believe that you are ...


1

You're misusing find and it's working accidentally. You see - by running: find *.txt What you're actually doing is invoking: find lect1.txt And you're accomplishing little more than echo. If however, you run find with a search criteria - so for example: find . -name '*.txt' Then it will traverse the current directory . and print any filename ...


6

grep searches for the first argument (the pattern) in the files passed on the command line or stdin if no files are passed. Without the quote your shell will expand lect* to all the files in the directory that begin with lect. Your command will then be: grep lect1.txt lect2.doc lect3.doc which means search for the text lect1.txt in both .doc files. ...


1

find *.txt | grep lect* You generally want to give a path name as the first argument to find(1), rather than a file name. It certainly is not a file name pattern. The shell is expanding *.txt into the name of all *.txt files in the current directory before calling find, which isn't what you want here. More useful variants of that command are find . ...


30

grep by default searches standard input if no files are given: grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. If you just do grep doc grep expects standard input to ...


12

grep is waiting for input. From man grep: [...] DESCRIPTION grep searches the named input FILEs (or standard input if no files are named [...]


1

It is not possible by only using grep. You have to use another tool e.g. sort: $ grep -e apple -e mango *.txt | sort -t: -k2,2 1.txt:apple 3.txt:apple 2.txt:mango 4.txt:mango


0

grep -r --include "httpd.conf" "10.22.0.141" . | cut -d: -f3- |\ cut -d ' ' -f4 | sort | uniq -c


0

Try: $ case "$(head -n 1 < file)" in (*pattern*) echo Match ;; esac Match


0

Do you want the list of full strings as a result, or do you want a list of files that contain the string? This would search line number one(-n1) of shell scripts(*.sh) and collect the strings containing 'bash': head -n1 *.sh | grep bash > fullstring.txt fullstring.txt would contain something like this: #!/bin/bash #!/bin/bash


0

Here is a perl onliner to do just that perl -ne 'print if /MY_SEARCH_STRING/; exit' myfile.txt This is going to check if the keyword MY_SEARCH_STRING is present in the first line of the file myfile.txt. If you need to search in the entire file just remove exit from the oneliner.


0

Your question wasn't completely clear. If you want just the first line of the output from ifconfig, you can use this: ifconfig | grep -m1 = If you need all the information for the first interface you can do this ifconfig | grep -A7 -m1 =


3

$ file corncob_lowercase.txt corncob_lowercase.txt: ASCII text, with CRLF line terminators Probably the DOS line endings are the source of your problem. CR is counting as a character for at least some purposes. Run it through dos2unix, or tr -d '\r', before greping.


0

I have implemented the comment of @Rob and succeeded to get the desired result. Replace string by your string. grep -Rin "string" . | grep ":1:.*string" > result.txt This does a recursive case-insensitive search for string in the current directory and prints the line numbers. Then it searches for occurrences in files which are on line 1 and saves the ...


0

Here is a (bad) example of a perl-script that would do something like this: #!/usr/bin/perl -w foreach (@ARGV) { my $filename = $_; open my $file, '<', $filename; my $line = <$file>; close $file; print "$filename\n" if $line =~ /your-match-text/; }


1

PID=`ps -ef | grep ${SCRIPTNAME} | head -n1 | awk ' {print $2;} '` This could either return the PID of your script, or of the grep process, or of some unrelated process that happens to have a similar enough name. This is likely to be the cause of your problem: sometimes your script writes its own PID to the pidfile and sometimes it writes something else. ...


2

In awk you can do: awk 'x && $1!=x {print y; print}; {x=$1; y=$0}'


1

Use find to do all the filename filtering.  Rather than find . -name "*.cpp" | grep "foo" | xargs grep … do find . -name "*.cpp" -name "*foo*" -print0 | xargs -0 grep … If you want to do something slightly more complicated, like find . -name "*.cpp" | egrep "foo|bar" | xargs grep … you can do find . -name "*.cpp" "(" -name "*foo*" -o -name "*bar*" ...


2

If you have to jump through alot of hoops, then the efficiency of xargs is lost anyway. Here is one crude work around: find . -iname "*.cpp" | grep "<pattern>" | while read -r x; do grep exa "$x"; done Every time I run into problems with spaces in file names, the answer is double quotes on a variable.


2

This should work even without GNU tools: #Find all C++ files that match a certain pattern and then search them find . -name "*.cpp" \ | grep "<name regex>" \ | perl -pne 's/\n/\0/' \ | xargs -0 grep "<content regex>" The perl call replaces line breaks with null characters, which will allow xargs -0 to interpret the input on a per-line ...


5

Use something like this perhaps (if gnu grep). grep -r 'content pattern' --include==*.cpp man grep --include=GLOB Search only files whose base name matches GLOB (using wildcard matching as described under --exclude) Also see the options for null delimiters. -Z, --null Output a zero byte (the ASCII NUL character) instead of the character that ...


-1

grep -v "^\s*[#;]" any.conf | grep -v "^\s*$" that is what works for me. ignore commented or empty lines, even whitespace before hash mark or semicolon


1

tdate has a space in there. So, grep treats the second part of tdate as a filename. You will need to enclose $tdate in double quotes to prevent that. Your command should look like: cat /var/log/secure | grep "$tdate" | grep 'servername su' >> $attempted_su_log


2

You have to quote the argument to grep: grep "$tdate". This is because $tdate expands to two whitespace-separated words, which in turn get passed to grep as two arguments, unless the double quotes are added. Your script could be improved to remove the useless use of cat, and to call grep only once: </var/log/secure grep "$tdate.*servername su" ...


0

I already gave you my FastaToTbl and TblToFasta scripts in another answer. Using them, you could do FastaToTbl sequences.fa | awk 'length($2)>200' | TblToFasta > newfile.fa FastaToTbl #!/usr/bin/awk -f { if (substr($1,1,1)==">") if (NR>1) printf "\n%s\t", substr($0,2,length($0)-1) else ...


0

I will assume that you need to grep for a particular pattern through text files and recurse through subdirectories. If you are using GNU grep, it is pretty simple: cd /to/the/top/level grep -r "PatternToSearch" * Instead of -r, you can use -R or --recursive You must use quotes if you have spaces in your search pattern. There are a lot of options to ...


1

You could also use head + grep and group the commands with {...} to share the same input: { head -n 50 >/dev/null; grep PATTERN; } <infile that way head gets only the first n lines, dumps them to /dev/null and the remaining lines are processed by grep.



Top 50 recent answers are included