Regular expressions are a means of matching a pattern of characters within a string.
13
votes
3answers
437 views
What is the difference between [[ $a == z* ]] and [ $a == z* ]?
Is there is any difference between these two.
[[ $a == z* ]]
and
[ $a == z* ]
Can I have an example where they would have different outputs?
Furthermore, how does the working of [[ ]] differs ...
12
votes
3answers
4k views
Can grep output only specified groupings that match?
Say I have a file:
# file: 'test.txt'
foobar bash 1
bash
foobar happy
foobar
I only want to know what words appear after "foobar", so I can use this regex:
"foobar \(\w\+\)"
The parenthesis ...
5
votes
1answer
344 views
Why are capital letters included in a range of lower-case letters in an awk regex?
$ echo ABC | awk '$0 ~ /^[a-b]/'
ABC
$ echo ABC | awk '$0 ~ /^[a-a]/'
$ echo ABC | awk '$0 ~ /^a/'
$
You see. /[a-b]/ captures A, but /[a-a]/ or /a/ doesn't. Why?
6
votes
3answers
654 views
Replacing Multiple blank lines with a single blank line in vim / sed
Question more or less says it all. I'm aware that /^$/d will remove all blank lines, but I can't see how to say 'replace two or more blank lines with a single blank line'
Any ideas?
5
votes
3answers
12k views
How to use regrex with AWK for string replacement in this example?
Suppose there is some text from a file:
(bookmarks
("Chapter 1 Introduction 1" "#1"
("1.1 Problem Statement and Basic Definitions 23" "#2")
("Exercises 31" "#30")
("Notes and References 42" "#34"))
)
...
4
votes
1answer
171 views
Match word containing characters beyond a-zA-Z
To match a word one can use
\v(\w+)
From the vim help :h \w:
\w word character: [0-9A-Za-z_]
This works exactly as described in the manual. However, I want to
match words that contain ...
3
votes
3answers
675 views
In a regular expression, which characters need escaping?
In general, which characters in a regular expression need escaping?
For example, the following is not syntactically correct:
echo '[]' | grep '[]'
grep: Unmatched [ or [^
This, however, is ...
3
votes
2answers
3k views
sed one-liner to delete everything between a pair of brackets?
I am working with some text that is full of stuff between brackets [] that I don't want. Since I can delete the brackets myself, I don't need the one-liner to do that for me, but I do need a one-liner ...
1
vote
4answers
336 views
Replace text between brackets
I'm using awk '{ gsub(/BAR|WIBBLE/, "FOO"); print }' to replace text in data like:
SOMETHING [BAR, WIBBLE]
SOMETHING [BAR]
This gives the desired result of:
SOMETHING [FOO, FOO]
SOMETHING [FOO]
...
1
vote
4answers
333 views
Stripping all vowels but the first from a set of strings
I have a string comprised of multiple substrings, separated by underscores. For example: AbcdAEfd_hEgdgE_AbAAAAA. I need to remove all vowels except the first from each substring. So:
AbcdAEfd -> ...
26
votes
4answers
7k views
What is the difference between `grep`, `egrep`, and `fgrep`?
Can any one tell me the technical difference between grep and egrep, and fgrep and provide a suitable example?
When do I need to use grep over egrep and vice versa?
12
votes
9answers
1k views
There must be a better way to replace single newlines only?
I am in the habit of writing one line per sentence because I typically compile things to LaTex, or am writing in some other format where line breaks get ignored. I use a blank line to indicate the ...
8
votes
2answers
497 views
What is the definition of a regular expression?
I recently got into a friendly argument with Ghoti about what constitutes a regular expression in the comments to my answer to this question. I claimed that the following is a regular expression:
...
6
votes
4answers
12k views
Finding text between two specific characters or strings
Say I have lines like this:
*[234]*
*[23]*
*[1453]*
where * represents any string (except a string of the form [number]). How can I parse these lines with a command line utility and extract the ...
5
votes
1answer
282 views
Why do some regex commands have opposite intepretations of '\' with various characters?
Take, for example, this command:
find . -regex ".*\.\(cpp\|h\)"
This will find all the .h and .cpp files in your directory. The period character '.' in regular expressions usually means "any ...
3
votes
2answers
187 views
Find and regex
What am I doing wrong with this find expression?
; touch ook ooks
; find . -name 'ook' -or -name 'ooks' -type f
./ook
./ooks
; find . -name 'ook[s]?' -type f
[returns nothing]
; echo $?
0
2
votes
4answers
2k views
How can I find all files in a folder that contain a match of a regular expression in the file name?
I'd like to find all of the files in my home folder on Linux (Ubuntu, in this case) that contain a match a particular regular expression. Is there a simple Unix command that I can use in order to do ...
2
votes
1answer
206 views
How to join vCards lines
vCard uses a special way to split long lines: At 75 characters, insert a DOS newline and a space. Joining therefore means to replace all occurrences of the sequence "CR, LF, space" with an empty ...
2
votes
1answer
246 views
Trouble with grep -o regex
I'm trying to use a grep command with the --only-matching flag, but it's not behaving as I would expect it to.
This command:
echo "1/2/3/4/5" | grep -oE "^([^/]+/){0,2}"
Gives this output:
1/2/
...
1
vote
2answers
1k views
Find all lines in a file with a certain character at a certain position
I have a lab question asking me to use grep with a regex pattern to match every word where the third letter from the beginning of the line is an "a" and save it with a redirect. How can I do that?
1
vote
1answer
487 views
Difficulty making a regular expression to find at least 2 occurrences of a character in a file
I have a file containing random codes. Each code has ten characters in it, and I am trying to grep codes in the file that have at least 2 occurrences of a character. I am doing this:
grep DD* ...
0
votes
1answer
47 views
How to ask for specific parameters with grep?
I need to look in /usr/share/dict/words for a 5-letter word that starts with d or D, followed by a lowercase vowel, and ends with s. I have tried everything but to no avail.
