Regular expressions are a means of matching a pattern of characters within a string.
1
vote
2answers
61 views
Using a variable in a regex
How can I use a variable string in a regular expressions
while read line
do
if [[ line =~ *"$key"* ]]; then
echo line
fi
done < "$filename"
$key is a string ...
1
vote
2answers
91 views
Differing outputs of the commands “ls .*” and “ls *.”
The command
ls .*
when run gives as output the following :
All the files in the current directory starting with a . (hidden files)
All the files in the hidden directories present in the ...
1
vote
2answers
26 views
use search result in replace statement with vim substitute
I know this is gonna be a stupid question, but I've tried here, google and the official documentation.
I've got a file with a whole heap of words of the form 'foo' and i want to change them to ...
0
votes
3answers
43 views
Ignore lines containing a capital letter
Why does the following command still return some lines containing a capital letter?
aspell dump master | grep ozz | grep -v '[^A-Z]'
2
votes
2answers
48 views
Understanding -regex with GNU find
Background
I have what I think should be a simple case. I want to find all files with "cisco" in the name and do something with those files (via xargs).
Finding files with ls
Before I use xargs, ...
3
votes
1answer
27 views
rsyslog: make %syslogtag% equal length
I have rsyslog configured to log in the following format:
$template long-iso,"%$NOW% %timereported:8:15% %syslogtag% %msg%\n"
which produces logs like this one:
2013-12-14 15:59:39 ...
1
vote
0answers
20 views
String with at least one uppercase, one lowercase, one special char, one digit and one white space [migrated]
I want a script to select a username. The rules for selecting a username are:
The minimum length of the username must be 5 characters and the maximum may be 10.
It should contain at least one letter ...
2
votes
1answer
83 views
Using BASH versus ASH on Synology
The majority of my photo files have the following naming convention / format IMG_20131204_120000.JPG, showing date and time the photo was taken (iPhone/Apple naming convention). However, not all my ...
0
votes
2answers
128 views
bash script, printing multiple lines that matching to a specific patern
I have a log file and I'm making a script to do some actions. An action is to print a specific area of the log.
Every block at the log starts with a specific time stamp and inside the block may have ...
1
vote
1answer
27 views
Help understanding an egrep expression
I stumbled across the following egrep expression:
egrep '^([^aieou]*[aieou]){5,7}[^aieou]*$' /usr/share/dict/words
on this page: http://matt.might.net/articles/sculpting-text
The expression is ...
2
votes
2answers
88 views
grep: regex only for match anything between parenthesis
How do I match word only between parenthesis
Input :
this is (test.com)
Desire Output :
test.com
3
votes
2answers
72 views
“grep: Unmatched [” error when using regex
I'm trying to find a pattern similar to this:
tail -n 100000 gateway.log | grep -B10 -A10 'Nov 22 11:13:56 Received Packet from [10.50.98.68'
Where "11:13:56" could be any time.
This is what I ...
0
votes
2answers
39 views
Regex and patterns on a ksh command line
I'm wondering what's the difference between using * and .* in a regex string.
I guess, * stands for "0 to n characters" but I don't see what .* stands for.
For example, what is the difference ...
3
votes
2answers
32 views
Extracting just a single group from a regular expression
I am trying to get a list of numbers from some hacked-together log files using zgrep. So, I am using:
zgrep -ie "total time: .* ms" *
This returns the whole line that I care about. I'd just like ...
1
vote
2answers
48 views
Ensure /etc/export contains rule using bash
What's a working example of modifying /etc/export using bash?
Example:
I want to ensure the following rule is present and valid:
/export/home 2400:123::/32(rw,no_root_squash)
And it should:
Add ...
6
votes
2answers
174 views
Why two almost idetical grep commands return different output: w/o and with filename
I have 2 almost identical greps:
[Alex@localhost tmp]$ grep /bin/bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
AlexL:x:500:500::/home/AlexL:/bin/bash
user1:x:501:501:user1 ...
2
votes
2answers
56 views
Print some fields of /etc/passwd
I have a question concerning the usage of wild cards in Linux. Here I have some lines from a long list of lines in the /etc/passwd -file.
root:x:0:0:root:/root:/bin/bash
...
1
vote
2answers
79 views
What does this regex mean?
I was working on the conversion of Unix newline to Windows newline. I tried to unix2dos but it gave me some binary error, so I looked it up and stumbled upon this regex
sed 's/$'"/`echo \\\r`/" ...
3
votes
3answers
104 views
sed - how to (not) match unmatched brackets
How can I do my operation only on lines that close their brackets?
For example I want to change ".is" lines and add a bracket but not for lines with unclosed brackets.
so these are changed
...
0
votes
1answer
34 views
sed how to do “or”
I have the following two lines that work ok:
/\.to <\=/ s/<\=/be <\=/
/\.to_not <\=/ s/<\=/be <\=/
I would like to combine them and I tried:
/\.to( |_not )<\=/ s/<\=/be ...
1
vote
3answers
72 views
a string representation using regex (modified)
I would like to use regular expression to represent a pattern with the following type of characteristics
1) It includes a continuous sequence of digit values. The length of this sequence it at least ...
0
votes
2answers
165 views
sed - how to exclude multiple patterns in the match
I have currently a set of seds that's working well except for one thing.
The particular expression I'm having an issue with is:
sed -i '/[^}.to]\.to[[:space:]]/ s/\(\S\)/expect(\1/' ...
3
votes
1answer
67 views
Why doesn't this regex work?
I want to look at the output of git status -s to determine whether there are any untracked files in the tree. I tried the following test, but the regex doesn't match. Why not?
$ git status -s
## ...
2
votes
2answers
59 views
Trouble getting regex to work with find
I'm having a lot of trouble getting find to work with regex flags. Im using egrep on its own to find a date pattern in a file, like this:
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2}'
which will return dates ...
1
vote
0answers
45 views
lftp exclude syntax confusion
I am confused with the syntax description given for lftp from their website:
-x RX, --exclude=RX exclude matching files
-X GP, --exclude-glob=GP exclude matching files
How ...
1
vote
1answer
54 views
How to Replace Path in Search field using VIM
I'm trying to replace this text in VIM
Actual Path /home/omipenguin/Servers\ Information/systemscript.sh/sysinfo.txt
with this new path
/home/sysinfo
I tried
%s/\/home\/omipenguin\/Servers\ ...
5
votes
2answers
103 views
Lowercase all but the first (Uppercase) letter from UPPERCASE in cyrillic
To make all letters a lower case except the first letter. The first letter would look like "Uppercase" after I changed (from UPPERCASE in cyrillic). The rest (not UPPERCASED) leave unchanged.
I'm ...
8
votes
2answers
808 views
Regular expression in bash script
This is my first time bash scripting so I'm probably making an easy mistake.
Basically, I'm trying to write a script that gets the groups of a user, and if they are in a certain group, it will log ...
3
votes
3answers
84 views
Using a perl compatible regex with GNU grep -P
I'm using this regex (?<=\[')[^,]* on a file containing the following line disk = ['OVS/sdasd/asdasd/asdasd/something.img, w']
I want that to return OVS/sdasd/asdasd/asdasd/something.img
How do I ...
1
vote
1answer
70 views
Brace expression does not work in find regex
I have two folders, CA01 and CA02 under current folder, foo:
foo -+
|
+-CA01
|
+-CA02
When I type
find . -regex ".*CA[0-9]+" -exec echo {} +
Or
find . -regex ...
1
vote
2answers
64 views
Why `rename` behaves differently whenever I use full path instead current path?
Ok, I'm answering a question where the OP have several repositories in Ubuntu that might be causing a problem installing unrelated software. I recommend disabling the PPA's using a single liner rename ...
3
votes
1answer
154 views
Get all regex matches between two patterns and print them to file
I've got a file with a bunch of long lines. I'd like to grab every group between two patterns and print them to a new file, one match per line. I could manage to do this with Python, but I'd prefer ...
2
votes
1answer
61 views
Regex to find NFS filesystems that aren't being mounted with nosuid, except for ones with special needs
I have a script that runs the command mount -v 2>/some/file, which I then parse with another script using the following regex:
^.*[\ \t]+type[\ \t]+nfs(?![\ \t]+.*\b(?:nosuid|nosetuid)\b).*
The ...
0
votes
2answers
78 views
grep + operator
According to the grep manual:
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
Let's test it
echo 'agb' | grep 'a.*b' # returns ...
0
votes
2answers
27 views
How can I reformat tabular data using AWK?
I have a file contain following output.
ora.abc.db
1 ONLINE ONLINE serverA Open
2 ONLINE ONLINE serverB Open
ora.xyz.db
1 ...
4
votes
2answers
116 views
VIM substitute occurrences of pattern after a particular word in a line
I have such lines in my text.
text before pattern = garbage** text after pattern is pattern
If in VIM I do %s/pattern/saturn/ it replaces all occurrences of pattern. But I only want to replace ...
2
votes
4answers
69 views
Alternative regex for {}
I am trying to replace all of this line, but the numbers:
looktype="123"
so only the numbers show up.
Is this possible in any easy way?
{sub ("look type=\"[0-9]{0,3}", "TEST")}
I am trying this ...
1
vote
1answer
41 views
How to match a literal '*' with sed?
I have this line in my file called test.php
'dbs_password' => 'a8b*cyP0',
and want to replace into:
'dbs_password' => 'password-here',
So I use this sed command with syntax:
$ sed -e ...
1
vote
2answers
52 views
Pattern repetition and regexp
Take the pattern
[UGLER]*
Can the string UUG match against it? I mean to say, is repetition allowed?
2
votes
2answers
101 views
regular expressions with grep
I have this command right here:
grep -B10 -A10 '14:14:50 {"channels":["/alerts/6979/new"],"data":"New alerts for unit 6979"}' mygateway.log
Rather than searching for 14:14:50 I would like the 50 to ...
1
vote
3answers
50 views
Stuck in separating chars into strings in my approach. Found a working approach but can not understand it
I am trying to figure out how to break a string to be on character per line.
I.e ahebhaaa to be:
a
h
e
b
h
a
a
a
I tried:
$ echo ahebhaaa | sed 's/\(.\)\(.\)/\1\n\2/g'
I.e. my ...
4
votes
2answers
109 views
Why do I need to escape regex characters in sed to be interpreted as regex characters?
It seems e.g.
cat sed_data.txt | sed 's/\b[0-9]\{3\}\b/NUMBER/g'
that I must escape characters to form a regular expression. In this case I had to escape braces in order to be interpreted as a number ...
3
votes
3answers
113 views
vim search numbers containing specific number of digits
I need to find specific length numbers in a big document. I tried to use regex for this. For example, If I need to search for numbers with exactly 2 digits, I use /d/d (i.e. /d twice followed by a ...
1
vote
2answers
55 views
sed : Ignore line starting whitespace for match
I want to replace the default listening port of httpd to 9090. I can edit the line in httpd.conf file using below
sed -i "/^Listen/c\Listen 9090" /etc/httpd/conf/httpd.conf
But the line
Listen 80
...
1
vote
3answers
113 views
grep for an alphanumeric strings of any length with a colon on each side
How would you grep for an alphanumeric strings of 1 to 50 characters (ideally, any length would work too) with a colon on each side – a typical result would be all the lines containing the string ...
2
votes
1answer
108 views
How to insert both vimscript variable and string into vim search and replace
I've only just found out about Vimscript's existence as of today, so forgive me if this question is more basic than it seems, but I've been struggling to find an answer to this question for the better ...
1
vote
1answer
54 views
Aptitude regex engine
I was trying to lookup the versions of libdb in the debian archives with something like that:
$ aptitude search '~S~ndev$(~nlibdb~(\d~|-~)!~v~rnative)'
which didn't work as expected so I changed ...
0
votes
4answers
318 views
Match multiple regular expressions from a single file using awk
I'm trying to parse a HTML file using shell scripting.
There are 4 different regular expressions that i need to catch: name= , age= , classs= , marks=.
Using
grep "name=\|age=\|class=\|marks=" ...
1
vote
2answers
84 views
How can I combine grep with file and extra regex
I have a file of strings in each line.
For example:
AAAAA
BBBBB
CCCCC
etc
I want to check each string one by one whether it exists in a series of files in a specific directory.
I ...
1
vote
4answers
89 views
Regexp on data flow
I want to test if in my data flow, there is something, and do an exit code.
I use gammu monitor, which provides a data flow, for example :
[root@MYSERVER ~]# gammu monitor
Press Ctrl+C to break...
...