Regular expressions are a means of matching a pattern of characters within a string.
1
vote
2answers
29 views
Using a shell variable with grep
I have a list of tokens in a text file and want to use grep to get the lines from a second text file that contain those tokens, but seem to be having trouble accessing the shell variable with grep:
...
1
vote
1answer
17 views
Regex quantifiers are not working well with locate
This output is rather self-explanatory:
XXXXX@debianvirtualbox:~$ locate -r "^/var/lib/tomcat[0-9]/.*" -l 10
/var/lib/tomcat8/conf
/var/lib/tomcat8/lib
/var/lib/tomcat8/logs
/var/lib/tomcat8/webapps
...
4
votes
4answers
68 views
egrep regular expression for times over five minutes
I have the following time formats in a text file
`1` equals one second.
`5|01` equals five minutes and one seconds.
`13|01` equals thirteen minutes and one seconds.
`21|12|01` equals 21 ...
1
vote
2answers
45 views
How to make + work in sed's regexps on FreeBSD?
Consider the following typescript:
$ freebsd-version
10.0-RELEASE-p5
$ echo ' found' | sed -n '/[[:blank:]]\+/p'
$ echo ' found' | grep '[[:blank:]]\+'
found
When I do this on Arch Linux with GNU ...
4
votes
3answers
126 views
grep regular expression solution (greedy not working)
I have the following text in the data.txt file
:MENU1
0. public
1. admin
2. webmail
:SYNTAX
! opt1, ... :
:ERROR1
Error #1, blah... blah.. blah...
Please do ...
:ERROR2
Error #2 ...
and I want ...
2
votes
2answers
28 views
Cleaning PHP exploits from infected files using sed
Recently my server has been a target for a series php exploit attacks and using ClamAV I was able to identify many infected files.
The catch here is that there are legitimate files that are infected ...
0
votes
0answers
23 views
swap strings of text inside vim using regex
I am working with a file where I need to swap "string A" and "String B's" locations simultaneously with a regex inside the vim. Here is an example line of code:
succ_1st=`grep 'resulted in successful ...
0
votes
1answer
20 views
Match everything after backslash and before space
I have strings similar to the following:
*unknown*\*unknown* (8)
hello\morning (3)
I'm trying to match just morning or *unknown\*.
So far I have tried:
[^\\]+$
But that matches from backslash ...
9
votes
5answers
388 views
How to print file content only if the first line matches a certain pattern?
I am writing a script, I want to check if the first line of the file matches a certain pattern and if it does then print out the file. How can I achieve this?
How do I check for pattern? Is there a ...
-3
votes
1answer
72 views
Regular expression: when every alternate character is a digit
I am trying to write a Linux command using egrep that validates lines whose even character is a number.
I am doing it this way:
egrep "^(.[0-9])*$" text.txt
However, this fails for i) empty ...
0
votes
1answer
21 views
subtract dates from two files for same process
I have initiated 50 processes and in one file "say start_time", I have stored when process was started and in another file "say end_time", I have stored when process finished.
files are like below -
...
1
vote
2answers
50 views
how to swap text inside files [duplicate]
Inside my file I need to swap places of all instances of :
grep 'search string'
and
sed '/string of text/,/string of text/{//!d}'
In VI I have tried a regex with no luck - it says something ...
0
votes
1answer
17 views
Is there a way to get all the NEWS.gz which is/was this year in /usr/share/doc/ of $PACKAGENAME.
If people go into /usr/share/doc/$PACKAGENAME (you can use any valid package name which you have installed on your system) you will see that the directory has various files in it, at the very least ...
-3
votes
1answer
37 views
Regexp pattern - is it correct and what is its purpose? [closed]
Is this pattern correct?
logPattern = ^NAME.log(-\d+)?$
What is the purpose of this?
1
vote
1answer
22 views
Including or in grep pattern
I want to grep a word wrap in single quote or double quote
eg.
"myWord"
'myWord'
I am using
grep -rn "\"\|\'myWord\'\|\""
this should allow "myWord" , 'myWord" , "myWord' and 'myWord' , but ...
1
vote
1answer
31 views
Why is vi (but not cat) showing ^[[31m[^[[m^[[31m" after I use sed and output to a file I then edit? [closed]
When I use
cat Variables/user-extensions.js | sed -e 's/css/XXX/'
on an input file of the form
storedVars["css_body"] = "css=body";
storedVars["css_main"] = "css=main";
I see output such as
...
1
vote
1answer
100 views
Sed replace portions of lines not beginning with http://
I'm getting stuck with sed.. I'm trying to prefix links in an html file with a / when they don't start with http.. I'm aware this is possibly not the best way to go about this but I'm just after a ...
1
vote
3answers
54 views
parse credentials from PHP configuration file
I'd like to write bash script to parse data from a configuration file. I searched for this but without finding something I could modify to suit my needs.
Joomla! config file:
public $access = ...
4
votes
3answers
190 views
sed, convert single backslash to double backslash
I have a json string, which has a potpourri of doubly escaped/singly escaped newline chars. Json parser doesn't allow its string value to have single backslash escapes.
I need to uniformly make all ...
1
vote
1answer
36 views
grep -E regex syntax changed?
A lot of documentation I've read says that the following command should match lines where x appears at least 3 times in a row but no more than 4 times in a row:
grep -E {x}\{3,4}
E.g. ...
0
votes
1answer
29 views
logstash regex match in if condition
In logstash filtering, I have multiple tags setup based upon different error conditions and all the tags has a prefix, something like "abc:"
In the output, I want to send email based upon just ...
4
votes
1answer
325 views
Match pattern \\\" using grep
I have a json string inside json. This got encoded multiple times and I ended up with many escape backlashes: \\\".
The much shortened string looks like,
'[{"testId" : "12345", "message": "\\\"the ...
0
votes
3answers
62 views
Pulling out a certain value from a string via regex
I've see a bunch of examples, but I just can't seem to make this work.
Can grep output only specified groupings that match?
for example seems like it should work, but I get either errors or no output ...
2
votes
1answer
38 views
Double square parenthese inside grep pattern
grep "^[[:space:]]*clientPort[^[:alpha:]]" zoo.cfg
This is a grep pattern to extract the line that contains the port number from the configuration file.
1) Normally in a regex, I use square ...
0
votes
2answers
46 views
Use multiple commands in sed to insert text after string with special characters
I am having a little trouble trying to do a search and replace using sed in a config file.
I want to use multiple commands using the -e option to do the following.
Step 1: find the first occurrence ...
0
votes
3answers
46 views
How to search multiple search patterns from a file with grep
If I search for multiple search strings in grep : usually just do:
grep "search1\|search2" somefolder/*.txt
but, what if I have 100 or more search strings? Can I say like this:
grep ...
0
votes
2answers
36 views
Bash script to copy specific directories with pattern matching
How can I copy directories from one location to another location that matches a pattern.
For example:
source = /media/src/
destination = /media/dest/
The pattern is as /b/w{2}_/w{2}/b. i.e all ...
2
votes
1answer
45 views
Sed: replace text keeping digit occurrence
I have some text like:
Blablabla <b>[intlink id="</b>2204<b>" type="page"] Blalala
I want replace it for remove the </b> and <b>, keeping the id="number". So it should ...
0
votes
1answer
54 views
Need to capture part of file name into variable
I have bunch of CSV files named like so in a directory:
s_m_u_supplemental_fields_account_9_4_2015_11_26_Error.csv
s_m_u_supplemental_fields_account_9_4_2015_12_3_Error.csv
...
1
vote
2answers
41 views
Remove last comma within a pattern
If I have the following text within a file (somefile.txt):
CREATE TABLE "table_name" (
"id" int(11) unsigned NOT NULL,
"some_field" varchar(10),
);
CREATE TABLE "another_table" (
"id" ...
1
vote
1answer
15 views
Remove word after match
Consider the following line of text (taken from a MySQL insert):
"description" varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
I want to return
"description" varchar(255) NOT NULL ...
2
votes
1answer
47 views
Meaning of this character class in perl?
I am new with PERL(though I have good background in reg-ex), I was going through one perl script and it has one characters class
$alwayssep = "[\\?!()\";/\\|‘]";
Can anyone explain to me what is ...
2
votes
4answers
40 views
extract url between 2 strings in a file
I have a file in which each line is like this
"372"^""^"2015-09-03 06:59:44.475"^"NEW"^"N/A"^""^0^"105592"^"https://example-url.com"^"example-domain < MEN'S ULTRA < UltraSeriesViewAll�?...
4
votes
2answers
234 views
How to change all strings in python file from snake_case to camelCase in sed
I've tried some sed patterns like this from commandlinefu
sed -r "s/('[a-z]+)_([a-z])([a-z]+)/\1\U\2\L\3/"
But somehow it's not working. For one thing they forgot the digits, which I can fix, but ...
6
votes
4answers
241 views
How to add a header and/or footer to a sed or awk stream?
I have a bunch of output going through sed and awk.
How can I prefix the output with START and suffix the answer with END?
For example, if I have
All this code
on all these lines
and all these
...
1
vote
4answers
35 views
Match regular expression between delimiters
I have a file with several different lines. I'm looking for lines that contain(more or less) pattern like this:
\[.*<.*>.*\]
In other words, I want the lines that have <something> ...
0
votes
1answer
67 views
How to grep the following lines from a file?
I have a file which has the names of many files in a directory in the following format:
A20150824.0950-0955_jambala_CcnActiveSessionCounterJob
A20150824.0945-0950_jambala_CcnActiveSessionCounterJob
...
-1
votes
1answer
36 views
sed: regular expression,how to substitute?
I have those line on a text file
36)
SIGCANCEL
37)
SIGLOST
38)
SIGXRES
39)
SIGJVM1
40)
SIGJVM2
41)
SIGRTMIN
42)
SIGRTMIN+1
43)
SIGRTMIN+2
44)
SIGRTMIN+3
45)
SIGRTMAX-3
46)
SIGRTMAX-2
47)
SIGRTMAX-1
...
1
vote
2answers
54 views
Awk & regular expression to process pgn chess games file
Below an extract of the sample.pgn file
[Event "rated blitz match"]
[Site "Internet Chess Server"]
[Date "2015.08.20"]
[Round "?"]
[White "Villain"]
[Black "Hero"]
[Result "0-1"]
[WhiteElo "1643"]
...
2
votes
1answer
34 views
How to combine posix character classes of a regex?
I need to combine different character classes in a regular expresion used by sed. I need to match [:word:] and the minus symbol -. How does that look like? All my attempty failed at trying or ...
1
vote
0answers
34 views
what is the correct regexp for mysql filter in fail2ban?
I use the default filter file /etc/fail2ban/filter.d/mysqld-auth.conf for fail2ban service. But there are some errors in file /var/log/fail2ban.log as:
Found a match for '150815 10:42:54 [Warning] ...
1
vote
1answer
51 views
How to use sed to replace keys in a file using a regex?
I would like to modify a file to replace keys, suppose these keys are in the file as:
42NM
52NM
23NO
XNNM
I would like to replace anything with NM, with the word "Okay".
1 #!bin/bash/
2
3 if [ ...
4
votes
2answers
111 views
POSIX behavior of sed's ^ and $ regex anchors with multiline pattern spaces
What is the POSIX behavior of the sed ^ and $ anchors when used at the beginning and end of a regex if the pattern space contains embedded newlines, like after you use the N command?
Should they ...
0
votes
2answers
53 views
AWK, SED or GREP for extracting data from HTML file
Can anyone tell me which is best to use to extract the following data from a html file which was received with curl .
<script>document.getElementById("test-summary").innerHTML = ...
5
votes
3answers
46 views
Censor text with regex
I'm currently running this command to censor an indented to-do list.
sed -e 's/\(\s\+- \)\(.*\)/\1XXX/g'
It's great except that I'd like the number of Xs to match the number of matched characters. ...
0
votes
1answer
66 views
What's wrong with this grep?
> output2.txt
cd # some directory i'm trying to search
find views/shared -type f -name "*.js" -print0 | while IFS= read -r -d $'\0' line; do
echo -n "${line%.js}" | tee -a ...
1
vote
3answers
43 views
Remove words (letters followed by space) from a specific column
I have a file and the format is as follows:
cat dog AHF123432 | 123432 | dhfshfjdh
lion AFG23412 |23412 | dfshjhfjdhj
I need to remove those words from first column which contains only alphabets ...
3
votes
1answer
44 views
What is the effect of the underscore in [A-Z] [_a-zA-Z0-9]*?
In regards to regular-expressions what is the effect of the underscore in
[A-Z][_a-zA-Z0-9]*
1
vote
1answer
57 views
Bash script with “execute” command and strange regexp
So I was looking through Paul Irish's dotfiles and found reference to this bash script:
#!/bin/bash
cd "$(dirname "$BASH_SOURCE")" \
&& source 'utils.sh'
declare -a FILES_TO_SYMLINK=(
...
1
vote
1answer
37 views
How to express “There is a specified symbol in the following unknown numbers of characters” in regex(vim)?
I have many python code examples in my markdown files, which should be prepended with >>> symbols.
So I write a vim function to do this job:
function AddShellPrompt() range
let lnum = ...