grep

Printing out names of only directories and not files (BASH)

I have most of what I need so far I'm just unsure of the grep command to get only directories or if there isn't one. For context, this is the original request: This script should take a single command line argument which will be a path to a directory. (done) The script should make sure that the path is, in fact, a directory and that th...

How can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?

I have this script: #!/bin/bash CLASSPATH="/blah/libs/*:/blah/more/libs" CMD="java -cp $CLASSPATH MainClass" ALREADY_RUNNING_PID=`ps -ef --no-headers | grep $CMD | grep -v grep | awk '{print $2}'` if [ "$ALREADY_RUNNING_PID" ]; then echo "Already running" exit 1 fi $CMD & problem is it doesnt work due to the asterisk in th...

grep utility in shell script

I'm trying to overcome a limitation on our file structure. I want to grep a whole series of files in a known location. If I do a standard grep from command line (grep -i searchpattern known_dir/s*.sql) I get the following error: ksh: /usr/bin/grep: 0403-027 The parameter list is too long. so I have a little for loop that looks l...

Can grep show only words that match search pattern?

Is there a way to make grep output "words" from files that match the search expression? If I want to find all the instances of, say, "th" in a number of files you do; grep "th" * but the output will be something like (bold is by me); some-text-file : the cat sat on the mat some-other-text-file : the quick brown fox yet-another-text-f...

matching a line with a literal asterisk "*" in grep

Tried $ echo "$STRING" | egrep "(\*)" and also $ echo "$STRING" | egrep '(\*)' and countless other variations. I just want to match a line that contains a literal asterisk anywhere in the line. ...

How to find and replace all occurrences of a string recursively in a directory tree?

Using just grep and sed, how do I replace all occurrences of: a.example.com with b.example.com within a text file under the /home/user/ directory tree recursively finding and replacing all occurrences in all files in sub-directories as well. ...

Remove empty new lines in a text file via grep

FILE: hello world foo bar How can when remove all the empty new lines in this FILE? Output of command: FILE: hello world foo bar ...

List all files in a directory with abosolute paths (excluding directories)

I've currently got: ls -1 $(pwd)/* Gives me all the files in a directory with absolute paths - but formats it with the directory at the start of each list of files. Is there a way just to get a list of files in a directory recursively (absolute paths) - excluding the directory/sub-directories themselves? ...

Surround all lines in a text file with quotes ('something')

I've got a list of directories that contain spaces. I need to surround them with ' ' to ensure that my batch scripts will work. How can one surround each new line with a ' and a ' (quotes). e.g. File1: /home/user/some type of file with spaces /home/user/another type of file with spaces To File2: '/home/user/some type of file wit...

Replace two or more spaces within a text file with a ;

File1: hello world foo bar a word with a space I need to replace all white spaces which are two or more in length with a semi-colon(;). Result: File2: hello;world foo;bar a;word with a space ...

Display and pipe only a specific line of text from a stream

Here is a command line script for a dictionary lookup using Wordnet: #!/bin/bash # Command line look up using Wordnet - command line dictionary echo "Type in your word:" read word /usr/bin/curl -s -A 'Mozilla/4.0' 'http://wordnetweb.princeton.edu/perl/webwn?s='$word'&sub=Search+WordNet&o2=&o0=1&o7=&o5=&o1=1&am...

Remove a variety of lines in a text file

I've been trying to implement a bash script that reads from wordnet's online database and have been wondering if there is a way to remove a variety text files with one command. Example FileDump: **** Noun **** (n)hello, hullo, hi, howdy, how-do-you-do (an expression of greeting) "every morning they exchanged polite hellos" **** Verb **...

Reformat a large text file into one line strings (via BASH)

File1: hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you - world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life - I need to format this (for a huge list of words) into a term to definition format (one line per term). How can one achieve...

Using grep and ls in ftp client?

How could I use grep and ls in ftp client... I mean if I want to find some specific file I could use, ls -l | grep pattern thanks. jcyang. ...

grep multiple extension current and subfolders

I'm trying to grep multiple extensions within the current and all sub-folders. grep -i -r -n 'hello' somepath/*.{php,html} This is only grepping the current folder but not sub-folders. What would be a good way of doing this? ...

running grep from within GNU make

I need to find the text 'ifeq ($(Param1)' using grep. I try to assign search result to make variable. The problem is that single quotes don't escape text in make so when I try: GrepResult:= $(shell grep 'ifeq ($$(Param1)' TextFile) I get: Makefile:214: *** unterminated call to function `shell': missing `)'. Stop. The $ can be esca...

Using grep with two variables

I want to use grep with two variables in a shell script var = match cat list.txt | while read word_from_list; do grep "$word_from_list" "$var" >> words_found.txt done But grep expects to get a file as second input: grep: match: No such file or directory How can this be solved? ...

command-line grep for a string with '$' in it?

I'm trying to find where two variables are being concatenated in a directory of scripts, but when I try the following: grep -lire "$DATA_PATH . $AWARDS_YEAR" * I get "undefined variable" errors... I thought I could escape the $s by using: grep -lire "\$DATA_PATH . \$AWARDS_YEAR" * But I get the same error - so, how do you grep for...

Regular expression to match unique words in files

To prefix unique words with "UNIQUE:" inside a file I've tried to use a perl regex command like: perl -e 'undef $/;while($_=<>){s/^(((?!\b\3\b).)*)\b(\w+)\b(((?!\b\3\b).)*)$/\1UNIQUE:\3\4/gs;print $_;}' demo On a demo file containing: watermelon banana apple pear pineapple orange mango strawberry cherry kiwi pineapple lemon cranberry...

How to execute a command with one parameter at a time in the *nix shell?

Some commands like svn log, for example will only take one input from the command line, so I can't say grep 'pattern' | svn log. It will only return the information for the first file, so I need to execute svn log against each one independently. I can do this with find using it's exec option: find -name '*.jsp' -exec svn log {} \;. Ho...