Sponsored Content
Top Forums Shell Programming and Scripting Alternative command to grep -w option Post 302959204 by RudiC on Friday 30th of October 2015 05:13:01 AM
Old 10-30-2015
man grep:
Quote:
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
So Corona688's proposal should be slightly modified like
Code:
echo "ABC,123" | awk '$0 ~ "(^|[^A-Za-z0-9_])"P"($|[^A-Za-z0-9_])"' P="ABC"
ABC,123

---------- Post updated at 10:13 ---------- Previous update was at 10:11 ----------

Does your grep understand extended regexes? Try
Code:
echo "ABC,123" | grep -E "(^|[^A-Za-z0-9_])ABC($|[^A-Za-z0-9_])"
ABC,123

These 3 Users Gave Thanks to RudiC For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep alternative to handle large numbers of files

I am looking for a file with 'MCR0000000716214' in it. I tried the following command: grep MCR0000000716214 * The problem is that the folder I am searching in has over 87000 files and I am getting the following: bash: /bin/grep: Arg list too long Is there any command I can use that can... (6 Replies)
Discussion started by: runnerpaul
6 Replies

2. Shell Programming and Scripting

--alternative option in getopt

Hi, i need to use --alternative option of getopt for ex . getopt -o a:c: --alternative pw: -- "$@" if i use like this, i am not getting any output.Please help me how to correct this.i need to have a combination of long and short options.But long options have to begin with - and not... (0 Replies)
Discussion started by: padmisri
0 Replies

3. Shell Programming and Scripting

Alternative to grep

How to find a particular line in a file without using grep? (3 Replies)
Discussion started by: proactiveaditya
3 Replies

4. Shell Programming and Scripting

Need best grep option or alternative

Hello, I am processing a text file which contains only words with few combination of characters (it is a dictionary file). example: havana have haven haven't havilland havoc Is there a way to exclude only 1 to 8 character long words which not include space or special characters : '-`~.. so... (5 Replies)
Discussion started by: alekkz
5 Replies

5. Shell Programming and Scripting

-n option with grep command

Hi, what is the meaning of -n option before the grep command ? grep command searches for the specified string in the file tmp_crontab.txt but what does -n mean ? With Regards (1 Reply)
Discussion started by: milink
1 Replies

6. UNIX for Dummies Questions & Answers

alternative to the grep trick

Hi, We used to use the below commands often. ps -ef|grep bc ps -ef|grep abc|grep -v grep Both fairly returns the same result. For example, the process name is dynamic and we are having the process name in a variable, how we can apply the above trick. For example "a" is the... (11 Replies)
Discussion started by: pandeesh
11 Replies

7. Shell Programming and Scripting

Please suggest alternative to grep

Hi Experts, PFB my requirement: I have a file (named file1) containing numbers like: 372846078543002 372846078543003 372846078543004 372846078543005 372846078543006 I have another file (nemed file2)where lines containing these numbers(present in file1) are present; Eg: lppza087; <PERFB >... (6 Replies)
Discussion started by: niladri29
6 Replies

8. Shell Programming and Scripting

Maxdepth command not working in AIX.Need alternative solution for this command

Hi All, I am trying to select 30 days older files under current directory ,but not from subdirectory using below command. find <Dir> -type f -mtime + 30 This command selecting all the files from current directory and also from sub directory . I read some documention through internet ,... (1 Reply)
Discussion started by: kommineni
1 Replies

9. Shell Programming and Scripting

Help with grep, or alternative

say I have a big list of something like: sdg2000 weghre10 fewg53 gwg99 jwegwejjwej43 afg10293 I want to remove the numbers of any line that has letters + 1 to 4 numbers output: sdg weghre fewg gwg jwegwejjwej afg10293 (7 Replies)
Discussion started by: Siwon
7 Replies
AGREP(1)						    BSD General Commands Manual 						  AGREP(1)

NAME
agrep -- print lines approximately matching a pattern SYNOPSIS
agrep [options] pattern [files] DESCRIPTION
Searches for approximate matches of pattern in each FILE or standard input. OPTIONS
Regexp selection and interpretation -e pattern, --regexp=pattern Use PATTERN as a regular expression; useful to protect patterns beginning with '-'. -i, --ignore-case Ignore case distinctions (as defined by the current locale) in pattern and input files. -k, --literal Treat pattern as a literal string, that is, a fixed string with no special characters. -w, --word-regexp Force pattern to match only whole words. A ``whole word'' is a substring which either starts at the beginning or the record or is preceded by a non-word constituent character. Similarly, the substring must either end at the end of the record or be fol- lowed by a non-word constituent character. Word-constituent characters are alphanumerics (as defined by the current locale) and the underscore character. Note that the non-word constituent characters must surround the match; they cannot be counted as errors. Approximate matching settings -D num, --delete-cost=num Set cost of missing characters to num. -I num, --insert-cost=num Set cost of extra characters to num. -S num, --substitue-cost=num Set cost of incorrect characters to num. Note that a deletion (a missing character) and an insertion (an extra character) together constitute a substituted character, but the cost will be the that of a deletion and an insertion added together. Thus, if the const of a substitution is set to be larger than the sum of the costs of deletion and insertion, direct substitutions will never be done. -E -num, --max-errors=num Select records that have at most num errors. -# Select records that have at most # errors (# is a digit between 0 and 9). Miscellaneous -d -pattern, --delimiter=pattern Set the record delimiter regular expression to pattern. The text between two delimiters, before the first delimiter, and after the last delimiter is considered to be a record. The default record delimiter is the regexp `` '', so by default a record is a line. pattern can be any regular expression that does not match the empty string. For example, using -d file ... defines mail messages as records in a Mailbox format file. -v, --invert-match Select non-matching records instead of matching records. -V, --version Print version information and exit. -y, --nothing Does nothing. This options exists only for compatibility with the non-free agrep program. --help Display a brief help message and exit. Output control -B, --best-match Only output the best matching records, that is, the records with the lowest cost. This is currently implemented by making two passes over the input files and cannot be used when reading from standard input. --color, --colour Highlight the matching strings in the output with a color marker. The color string is taken from the GREP_COLOR environment variable. The default color is red. -c, --count Only print a count of matching records per each input file, suppressing normal output. -h, --no-filename Suppress the prefixing filename on output when multiple files are searched. -H, --with-filename Prefix each output record with the name of the input file where the record was read from. -l, --files-with-matches Only print the name of each input file which contains at least one match, suppressing normal output. The scanning for each file will stop on the first match. -n, --record-number Prefix each output record with its sequence number in the input file. The number of the first record is 1. -q, --quiet, --silent Do not write anything to standard output. Exit immediately with zero exit status if a match is found. -s, --show-cost Print match cost with output. --show-position Prefix each output record with the start and end offset of the first match within the record. The offset of the first character of the record is 0. The end position is given as the offset of the first character after the match. -M, --delimiter-after By default, the record delimiter is the newline character and is output after the matching record. If -d is used, the record delimiter will be output before the matching record. This option causes the delimiter to be output after the matching record. With no file, or when file is ``-'', agrep reads standard input. If less than two files are given -h is assumed, otherwise -H is the default. EXAMPLES
agrep -2 optimize foo.txt outputs all lines in file foo.txt that match ``optimize'' within two errors. E.g. lines which contain ``optimise'', ``optmise'', and ``opitmize'' all match. DIAGNOSTICS
Exit status is 0 if a match is found, 1 for no match, and 2 if there were errors. If -E or -# is not specified, only exact matches are selected. pattern is a POSIX extended regular expression (ERE) with the TRE extensions. REPORTING BUGS
Report bugs to the TRE mailing list <tre-general@lists.laurikari.net>. COPYRIGHT
Copyright (C) 2002-2004 Ville Laurikari. This is free software, and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute this software under certain conditions; see the source for the full license text. BSD
November 21, 2004 BSD
All times are GMT -4. The time now is 09:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy