Tag Info

New answers tagged

1

One way would be to use egrep, or grep -E like this: rpm -qa | egrep -i '.*gcc.*' This uses the supplied search string as a regular expression where the period (.) stands for any character and the asterisk (*) stands for one-or-more of those characters. What I believe you're trying to use with rpm -qa | grep -i *gcc* is a feature of most shells called ...


1

Here's a variant on glenn's answer that will work if you have multiple consecutive occurrences (works with GNU sed only): sed ':x /"line"/N;s/"line"\n<second>/other characters/;/"line"/bx' your_file The :x is just a label for branching. Basically, what this does, is that it checks the line after substitution and if it still matches "line", it ...


1

sed '$!N;s/"[^"]*"\n[^>]*>/other characters /;P;D ' <<\DATA first "line" <second>line and so on DATA On every line but the $!last, sed pulls in the Next line and appends it to pattern space following a \newline character. If pattern space contains a string matching the pattern: "[^"]*"\n[^>]*> sed replaces it with the ...


5

read the whole file and do a global replacement: sed -n 'H; ${x; s/"line"\n<second>/other characters /g; p}' <<END first "line" <second> line followed by "line" <second> and last END first other characters line followed by other characters and last


5

Well, I can think of a couple of simple ways but neither involves grep (which doesn't do substitutions anyway) or sed. Perl To replace each occurrence of "line"\n<second> with other characters, use: $ perl -00pe 's/"line"\n<second>/other characters /g' file first other characters line and so on Or, to treat multiple, consecutive occurrences ...


1

Easy awk awk '/src/{a=$0}/foo/{b=1}b&&a{print a;exit}' If src or foo can be somewhere else in a different format or whatever awk '/^src/{a=$0}/"foo"/{b=1}b&&a{print a;exit}' If foo always comes after src awk '/^src/{a=$0}/"foo"/{print a;exit}' If there are multiple src blocks in a file and you want to print each one that contains foo ...


0

grep -o "Hello\|are" test | tr '\n' ',' -o to only output matching strings, then replace \n with ,.


0

You can try the following: grep -Eo "Hello|are" file | sed '$!N;s/\n/,/'


1

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use awk 'BEGIN{a=0} /^$/{if(a==1) print b; a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' We use the variable a to remember whether or not the pattern foo occurs in the input ...


3

> makes grep think the file is binary because it is binary. The thing is, you emptied the file, but didn't stop the program that was filling it. >output.txt creates output.txt if it doesn't exist, and truncates it to zero length if it does. At the point you run >output.txt, there is a tee process which has the file open. Truncating the file ...


1

You should note that egrep allows the 19 to be factored out, like such: grep -E '19(6[5-9]|[78][0-9]|9[0-6])$' filename which might be considered clearer.


0

$ (for i in {1900..2000}; do echo $i; done) | egrep '(196[5..9]|19[78][0-9]|199[0-6])' 1965 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996


8

Date ranges & regex aren't really that good a match. If I interpret the $ in your grep correctly the date is the last field on a line. Try this: awk '$NF >= 1965 && $NF <= 1996' filename If you must use grep it becomes more convoluted: grep -E '196[5-9]|19[78][0-9]|199[0-6]$' filename


2

who | grep -e "^$1" -e "^$2" -e "^$3" should do


1

Grep behaves differently depending on the type of regular expression engine it is using. By default it uses Basic Regular Expressions but can also use Extended Regular Expressions with -E and, on grep versions that support this feature, Perl Compatible Regular Expressions with -P. BRE can only search for two patterns, no more. The syntax is: grep ...


1

I'd recommend doing something like this: who | awk '{print $1}' awk will print the first column or the names of the users currently logged on. As terdon pointed out, to further narrow it down to three arguements: who | awk -v one=$1 -v two=$2 -v three=$3 '$1==one||$1==two||$1==three'


5

If the only problem is that grep treats it as binary, tell grep to search it regardless: $ head /bin/bash > out $ echo "test" >> out $ grep test out Binary file out matches $ grep -a test out test From man grep: -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option.


4

It might answer your question, so here are the results of a few tests I just ran: $ > output.txt $ file output.txt output.txt: empty $ echo "" > output.txt $ file output.txt output.txt: very short file (no magic) $ echo " " > output.txt $ file output.txt output.txt : ASCII text As you can see, the file isn't categorised the same way according ...


3

Only if the word contains characters that have special meaning to the shell. grep "Hello?" ./testfile will search for the literal string Hello? in the file. However, for example, grep Hello? ./testfile will search for the string HelloA if there is a file HelloA in your current directory, because ? will match any single character as a glob pattern. I'm ...


2

Most commands can deal with input that's either a file that they need to open for input, or as a stream of data that's passed to the command via STDIN. When the contents of cat file.txt is sent to another command through a pipe (|) the output via STDOUT that's passed to the pipe on the left side, is setup and fed to the command that's on the right side of ...


1

If you are open to other formats, consider: inc="hello|animal|atttribute|metadata" exc="timeout|runner" ssh machineB "grep -E '$inc' path/ptd.log | grep -vE '$exc'" Faster Alternative If your log files are large and you are grepping for fixed words, as opposed to fancy regular expressions, you may want to consider this approach: inc='hello animal ...


0

It may not seem possible but you can make use of grep's -f option to make use of that list of words, even though they're in a environment variable and not a proper file. The trick is in fooling grep into thinking that they are from a file like so: $ ssh machineB 'grep -f <(echo $wordsToInclude|tr , "\n") file1 file2 file3' This will run the grep ... ...


5

Neither ack or grep have any notion of a file's modification dates. For that you'll need to generate the list of files first, and then sort them based on afterwards. You can use xargs to run the output of either ack or grep into another command which will provide the modification dates. For the modification dates you can use stat to do that. Example $ ...


2

With GNU tools: grep -H something * | awk -F: '{"date -r \""$1"\" +\"%F %R\"" | getline d; print d,$0}' | sort -n Or, ack something | awk -F: '{"date -r \""$1"\" +\"%F %R\"" | getline d; print d,$0}' | sort -n This uses awk to separate out the file name from the usual grep output and awk calls date to retrieve the files date. grep is given the -H ...


0

My answer for the question in the subject, without storing pattern in a second file. Here is my test file: $ cat animals.txt cat 13123 23424 deer 2131 213132 bear 2313 21313 dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 GNU sed: $ sed '0,/^dog 123 4335$/d' animals.txt cat 13123 23424 deer 2131 213132 bear 2313 21313 Perl: $ ...


0

Here's the more verbose version of essentially the same thing @1_CR posted above and @ ash showed at pastebin, hopefully using more readable syntax: awk '{ lastLine = currentLine; currentLine = $0; } /foo|bar/ \ { getline currentLine next } NR > 1 \ { print lastLine; } END \ ...


1

Preceding the first regexp with a space: grep -E "\s+-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}" should do the trick, since it excludes the match at the beginning of the line. If you want just the 4th column, you can easily achieve that with either GNU sed: sed -r 's/^\S+\s+\S+\s+\S+\s+(\S+)(\s.*|)$/\1/' where \s is any whitespace character (space, tab and ...


4

Anchor the expression to the end of the line: grep -E "\-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}$" If you add the PCRE option of -o to return only the captured group, you will see that your example matches and returns the desired match: grep -oE "(\-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}$)" -146.17516 Too, to catch matches for lines in a file with optional, ...


0

If all the input files are formated the same then for a simple awk solution: awk '/^[a-Z]/ {print $1}' filename Using the carrat (^) will select only entries that have a character at the beginning of each line. Then only print the first field ignoring the =.


0

sed -e 'h;s/.*intron_[^:]*\(:[^[:space:]]*\).*/\1/;s/./ /g;;G;;s/\(.*\)\n\(.*\)intron_\([^:]*\):[^[:space:]]*/\2\3\1/' YourFile In 1 sed (no pipe) keeping the column. It use the holding buffer Posix version (so --posixon GNU sed)


1

You can use perl: $ perl -anle ' BEGIN {$" = "\t"} print "@{[@F]}" and next if $. == 1; $F[1] = $1 if /_([^:]*):/; print "@{[@F]}"; ' file id target_id length eff_length 1 FBgn0000721 1136 243.944268 1 FBgn0000721 1122 240.237419 2 FBgn0264373 56 0 3 FBgn0027570 54 0 Explanation -a: auto split each line into ...


5

Using sed and column: $ sed -E 's/ intron_([^:]*):[^[:space:]]*/ \1/' file | column -t id target_id length eff_length 1 FBgn0000721 1136 243.944268 1 FBgn0000721 1122 240.237419 2 FBgn0264373 56 0 The key part of this is the substitute command: s/ intron_([^:]*):\S*/ \1/ It looks for intron_ and saves everything after intron_ ...


0

Thank you for your detailed responses Letizia. I used bits and pieces of your code and some of your code, in turn, gave me more ideas. Just to not leave this post hanging: my final (gruesome) code ended up like this: cat sections/sem092 | sort -k 2 | awk '{ print $2 }' | uniq -c > no3 paste instructors | awk '{ print $2 " " $3 }' > no3n ...


1

Using awk: awk 'NF > 1 && $1 !~ /^\(|\)/ {print $1}' file Only print lines with at least one field (NF > 1), and ignore lines beginning with ( or ): ^\(|\).


2

It looks like you just want: grep -o '^[A-Z]\+' -o means only output the matching part of the line, and the regex matches a sequence of one or more capital letters at the beginning of a line. You can also do this sort of thing with sed, which is overcomplicated for this example but useful (and simpler than awk or perl) if you need to do more complex ...


3

That strings doesn't find the same patterns as your string is most likely caused by strings finding patterns at least 4 characters long and followed by a non-printable character. From man strings for GNU strings: For each file given, GNU strings prints the printable character sequences that are at least 4 characters long (or the number given with the ...


0

Why not: grep -a string file | strings strings prints the printable character sequences that are at least 4 characters long. So if you grep for something shorter than 4 characters, processing via strings first will cause it to miss out. Use strings -n 1 to set the minimum length to 1.


1

In double quotes, you need to backslash backslashes, i.e. double the backslash before the dot. system("grep '^.*\\.[a-zA-Z0-9][a-zA-Z0-9]*\$' file.txt > file2.txt"); # ^ # | # Here.


1

Save output of your command: cat sections/sem092 | sort -k 2 | awk '{ print $2 }' | uniq -c > firstPart.txt Save this line in file searchInstructorName.sh: cat $1 | while read line; do instructorID=`echo $line | awk '{print $2}'` name=`grep $instructorID instructorList | awk '{print $2 " " $4}'` echo "$line $name" done This script ...


0

You can use quickfix or errorfile feature in vim: $ grep -n foo * > /tmp/foo.list $ vim -q /tmp/foo.list Vim will open the first file in /tmp/foo.list and place the cursor directly in the line where foo was found. You can go to the next instance using :cn and previous instance using :cp. Side note: If you are already using vim or gvim, then I would ...


0

I'm not sure which version of grep you are using, but if I'm reading the man page of grep correctly, then the scanning will be stopped after the first successful match. Is that what you want? What I understood from your question was that you wanted to "open all files". If you don't mind using vim or gvim, then you can use this: $ grep -n mystring *.ext ...


0

As far as the matching is concerned, you'd need to be quite careful in order to build anything robust. For example, assuming that the first and middle names must start with a letter may contain hyphens (but no other punctuation or whitespace) your given-name pattern becomes something like [[:alpha:]][[:alpha:]-]*. You can then make an optional ...


1

Since the description part of the filename can contain the pattern - (a hyphen between two spaces), you can change that to some symbol that doesn't occur in the description part. I chose £, but that's purely arbitrary. rename 's/ - /£/' * rename 's/([^,]*), ([^£]*)£/$2 $1 - /' * s/ - /£/' tells rename to replace the first instance of - it finds with£. The ...


1

-exec takes the exit status of the command you put in it and uses it logicially within find So, just something simple like this should work find . -iname "*.ext" -exec grep -q "mystring" {} \; -exec open {} \;


3

If you have spaces in file names then you need to use print0 option for file, later -0 for xargs, and lastly -I {} for second xargs. find . -iname "*.maxpat" -print0 | xargs -0 grep -l "mystring" | xargs -I '{}' open '{}' Tested with emacs as an open command.


-1

Use pwd by find: find `pwd` -iname *.maxpat | xargs grep -l "mystring" | xargs open


2

It is due to GNU Parallel --pipe being slow. cat bigfile | parallel --pipe -L1000 --round-robin grep -f regexp.txt - maxes out at around 100 MB/s. In the man page example you will also find: parallel --pipepart --block 100M -a bigfile grep -f regexp.txt which does close to the same, but maxes out at 20 GB/s on a 64 core system. parallel --pipepart ...


0

The AIX version of grep supports a '-p' option, making this quite simple. pbsnodes | grep -p 'state = free' | awk '/procs/{n+=$NF;}END{print n;}' The '-p' option 'displays the entire paragraph containing matched lines'. (OS flavour was not specified.)


0

If for some reason you cannot use single quotes as suggested in Mikel's answer, you can temporarily turn off history expansion using set +H (turn it back on with set -H), as suggested by glenn jackman in comments.


0

You can parse the whole file into a Perl hash and print out the required fields (given as input arguments after the file name): perl -nle ' BEGIN{ $input_file = shift; $required_fields = shift } my ($field,$val) = split/:/; next unless defined $field; #Skip lines with no field names $fields{$field} = $val; END{ ...



Top 50 recent answers are included