Regular expressions are a means of matching a pattern of characters within a string.
0
votes
2answers
32 views
How to insert a substring to a line in a text file
I have a lot of C++ files, that have some lines like this:
CONST1( var1, var2, var3, var4 );
CONST1 is a constant that I know but var1 to var4 are variables and can be empty "". I would like to ...
0
votes
0answers
19 views
RegEx match + additional line removal
So I am pretty new to what I currently refer to as "advanced" RegEx, please pardon me, it's probably really easy for you guys but I need to be pointed in the right direction because right now I am ...
1
vote
3answers
41 views
sed works when input from echo but not from file
The following removes the surrounding quotes of the email address string:
$ echo "[email protected]" | sed 's/"([^"]*)"/\0/g'
[email protected]
But if:
$ cat ~/Desktop/emails.txt
"john....
1
vote
3answers
32 views
Extended Regular Expressions (EREs): How do I include pattern but exclude specific superset of pattern in matches?
The man page for the pgrep command indicates that the pattern "specifies an Extended Regular Expression for matching against the process names or command lines". Apparently, Extended Regular ...
10
votes
7answers
377 views
Is there a way to show only uncommented lines in a text file/script? [duplicate]
Many a times when manually grepping through a file, there are so many comments that your eyes glaze over and you start to wish there was a way in which you just could show it to display only those ...
0
votes
1answer
22 views
\1 in sed command not working under xonsh
I don't understand what I'm missing here:
$ echo 'testing' | sed -E 's/([a-z]*)ing/\1ing/g'
ing
I would expect the output to be testing again, since \1 should be test? That input seems to have been ...
-1
votes
0answers
28 views
Grep Regex does not work [on hold]
I have written a RegEx expression that should work but does not work with grep.
$ cat mission.sqm | grep -P '(?<=addons\[\]\=\n){([^}]*)}'
Sample data + result that I want to get:
https://gist....
1
vote
4answers
59 views
Regular Expressions to sanitize phone numbers
I am trying to write a script that uses sed and takes takes a text file containing phone numbers with area code 301 followed by 209( 3012093934, (301) 2093935, (301)209-3936 ). I have to remove the ...
0
votes
1answer
15 views
Get list of ZSH commands from ~/.zsh_history with regex separator and multiline fields [duplicate]
I want to get a list of all commands from the ZSH history in ~/.zsh_history. The file is formatted like so:
: 1467892191:0;cd /usr/share
: 1467892276:0;lsrc
: 1467892743:0;logout
: 1467892751:0;...
3
votes
2answers
42 views
sed and remove string between two patterns
I've got problem with removing part of string between two patterns with sed. I've always got last PATTERN-2 in line:
test.txt:
PATTERN-1xxxxPATTERN-2aaa
PATTERN-1xxxxPATTERN-2fffPATTERN-1zzzzPATTERN-...
2
votes
3answers
74 views
awk with regex for delimiter
I want to get the first word in every line from a file. Unfortunately a lot of lines begin with space(s). So I try to get the firs word with the following:
awk -F'[ \t]+' '{print $1}' < MyFile.txt,...
0
votes
1answer
37 views
How to backslash a dynamic string in bash
I want to backslash a variable automatically so that the end users don't need to type the backslashes for a Perl regex string replacement.
API_URI="http://something/api"
FIND="(API_URI)(.*?[\=])(.*?[\...
1
vote
2answers
56 views
bash regex extract key = value
I have a complex string of this form:
inp="key1 = what' ever the value key2 = the value Nb.2 key3= \"last value\""
I need to get the first key associated with its first value. I want to use bash ...
0
votes
1answer
20 views
error in order egrep
This is part of a script; this part is supposed to check if there is a string in the first word twice or more that is also in the last word twice or more (in a row at the last word).
echo "$first $...
-4
votes
1answer
53 views
Difference between ~ and ! in unix shell scripting [closed]
Which operator should be used between ~ and ! while checking a regular expression in IF condition in shell scripting??
Will there be any difference between the two cases shared below ??
case 1:
if [...
1
vote
1answer
24 views
regexp with aptitude part 2
I had asked about regexp with aptitude here and that worked beautifully. Now have i386 packages also in the midst. How do I make sure that i386 packages do not come into the picture. I tried the ...
1
vote
1answer
52 views
bash regex does not recognize all groups
I need to capture groups from a regular expression. But it seems I fail at grasping the concept of the bash variable BASH_REMATCH, as I can't get some groups. Here is my code:
# I want to get the ...
0
votes
0answers
23 views
how to use lynx to “print” a web page? WYSIWYG
When I printed a page, as per the manual:
Printing, Mailing, and Saving rendered files to disk.
Rendered HTML documents, and plain text files, may be printed using
the 'p' command while ...
0
votes
3answers
36 views
Extracting only specific multiple patterns from file
I have following file:
$less dummyKeyAndValue.txt
apiKey=key1;some_other_data;term=abc
apiKey=key2;some_other_data;some_other_data;term=def
term=pqr;some_other_data;apiKey=key1
apiKey=...
0
votes
0answers
20 views
Mass rename numbered extensions? [duplicate]
I have several files in the format of:
download.1
download.2
download.3
...
I'd like to rename all these in the following format:
download1.exe
download2.exe
download3.exe
Is there an easy way to ...
0
votes
3answers
53 views
How to capture contents of line up to certain string?
I have this output:
Never logged in.
Last login Wed Jun 22 15:12 2016 (BST) on pts/11 from host1
I need it without the (BST) on pts/11 from host1
just like that:
Never logged in.
Last login Wed Jun ...
9
votes
1answer
181 views
Is there an equivalent for vim's \zs in sed or perl?
In vim we can use the \zs atom to say "really begin the match right here":
:%s/funnyword\zs.*$/otherword/
Is there an equivalent for sed or even perl?
1
vote
1answer
34 views
sed replacement won't work (regex)
I'm trying to remove the text before the italian word "ANDATA" in the pdf file of a bus line.
I'm using this
pdftotext "file.pdf" - | sed -r "s/^.*ANDATA//g"
but this doesn't work. It only removes ...
0
votes
1answer
26 views
How to sed replace this end of the line?
I want to replace this end of the line [1-9] \\ \hline by [1-9] & \\ \hline where a new ampersand.
Data sample
The lorem ... & 1-2 & 3-4 & 5-6 & 6000 & 10000 4 \\ \hline
My ...
3
votes
1answer
61 views
Why does echo “a']” | grep -E “\'” not match?
Based on this question on SO, I have discovered that:
echo "a']" | grep -E "\a"
Matches OK.
echo "a']" | grep -E "\]"
Matches OK.
But:
echo "a']" | grep -E "\'"
Does not match. I cannot find ...
4
votes
3answers
166 views
Regex not matching file using `find` despite being valid [duplicate]
I am attempting to run this find command to match files with a camelcased name, with the goal being to put a space between adjacent lower and upper case letters, but it's not matching anything:
find -...
4
votes
2answers
50 views
how to select files in a directory with the boolean operator AND
I want to select (find/grep/ls) certain files in a directory starting with certain words and ending with a certain extension. Example:
bluebelt_hans_hoff.jpg
bluebelt_hans_hoff.JPEG
...
0
votes
1answer
42 views
grep a certain format, is this possible?
I'm trying to find a certain unknown 11 character sequence in a huge packet capture file. The one thing I do know is that the string I'm looking has format x.x-xxxxxxx. I've been looking for a while ...
2
votes
2answers
98 views
What exactly does this script do? [duplicate]
#!/bin/bash
for a in ./*.flac; do
ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
done
I found this script a few days ago to convert all FLAC files in the current directory into the MP3 format.
...
-1
votes
1answer
17 views
Linux replace only user part in email pattern
I have users like below in many *sh scripts.
[email protected]
[email protected]
[email protected]
I want to replace any emails like above with [email protected] in linux.
Any idea will be appreciated ?
3
votes
1answer
55 views
Edit several lines from a certain section of an INI file
I have the subversion config file (~/.subversion/servers)
I need modify to add proxy information (host, port, exceptions). This file contains many sections with proxy information. I only want to ...
1
vote
1answer
30 views
How to match string with regexp and conditionally do stuff with capture groups
The idea here is to change remotes from http to ssh in all git repositories
find / -type d -name '.git' 2>/dev/null | xargs -I {} $SHELL -c \
'cd $(dirname {}) && echo $(pwd) > /tmp/log....
0
votes
1answer
37 views
Perl command line RegEx not matching
So I have a file with contents in the following format,
randomString abc\/def/ghi/mno\/pq/r abc\/def\/ghi/mno\/pq/r
My intention is to replace all occurrences of / with \/ between two alpha-...
3
votes
5answers
205 views
Get first pattern matching in a line not using cut
having a bunch of texts similar to this (for the purpose of test and keeping data simpler as possible):
first 1 is the real 2 time the word matched 3
appeared on the previous line but is 4 the ...
0
votes
1answer
48 views
Ansible, attempting to get specific regex pattern to work with lineinfile module
As per my question above:
"Ansible, attempting to get specific regex pattern to work with lineinfile module"
Essentially, I'm trying to get it to work in a single play as opposed to 2 separate plays, ...
0
votes
1answer
42 views
Remove two columns of a SQL insert command in a dump file
I have a SQL dump file having several insert commands. I would like to remove, through sed or grep (or even another simple bash trick), a specific column and its equivalent data at the VALUES clause. ...
0
votes
1answer
26 views
Grep template for extracting lines where second word has only two vowels
For example, i have file with content
hello world
it's nice to see you
amazing night
what a wonderful day
my name is Robert
still breathing
speaking bottom soul
something wrong
I need to match those ...
-1
votes
1answer
28 views
How to change all “x” to “y” but only before (or after) initial “z” on each line?
On each line in a text file I need to change all . characters to # characters, but only before the first = sign. So, stop processing each line after the first = sign is encountered.
If easier, do the ...
0
votes
1answer
56 views
How to use grep or sed to extract multiple matches in the same line
I have a file that, amongst other things, has text like this:
<TR><TD>5</TD><TD>Ukraine</TD></TR>
<TR><TD>3</TD><TD>Vietnam</TD></...
1
vote
1answer
65 views
find and delete files older than specific days and have specific string in filenames
To delete files older than 5 days from a directory, we can use command.
find /directory -type f -mtime +5 -delete
But, in my case, I want to delete only those files having 'YYYY-MM-DD' in their ...
1
vote
2answers
29 views
How to use appropriate regex to find a pattern in awk?
Using the following example, I am trying to figure it out the regex which cover the following pattern in my awk program.
The output could be only numbers no more than 5 length : i.g. 15251
The ...
1
vote
4answers
85 views
Regular expression to match floating point numbers in shell script
I'm using a regular expression to match floating point numbers:
for f in $float_numbers ; do
if [ $f =~ "[0-9]*\.?[0-9]*" ] ; then
echo "****f is $f ****"
fi
done
where $...
1
vote
1answer
15 views
How to do Regexp/pattern-searching in gunzipped files?
How to use regexp/pattern-searching under gunzipped files. For instance ummm... let's use -
/usr/share/doc/linux-image-4.8.0-1-amd64$ zcat changelog.gz | less
Now the way I use is when reading the ...
0
votes
2answers
34 views
Sed and regex, same pattern on same line two different groups?
I have some data looking similar to this:
BLACK Harry<-George->Edna<-$$$Tom<-Tom->Phil<-Tue"
The data I want to use in this case is:
Harry<-George->Edna<-
Tom<-Tom-...
1
vote
1answer
31 views
KSH Pattern from variable does not work
Could somebody explain me why pattern in Korn shell (assigned to variable) works this way:
u@h:w$ pattern='file_[0-9][0-9]'
u@h:w$ ls $pattern
file_01 file_02 file_03
But it doesn't work for ...
0
votes
3answers
50 views
Multiple `awk` statements with pipes `|` ?
#!/usr/bin/env bash
#### Extract OS-related info from a Linux box ####
#### Display header message ####
# $1 - message
function write_header(){
local h="$@"
echo "------------------------...
0
votes
2answers
45 views
How can I replace opened brackets from end of line to new line using sed?
I have many files with wrong opened { brackets from end of line and need to replace it to new line by conditon [a-z)] [{]|[a-z)][{].
input example:
public class Test {
}
expected output:
...
2
votes
1answer
43 views
adding a regexp filter to block a subdomain and TLD in sendmail
Running Sendmail Version 8.15.2 on Fedora 24 and lots of spam is coming in from (subdomain).(manydomains).us. I'd like to add a filter like what's available in the access file to block and .us TLD's ...
4
votes
1answer
289 views
PCRE-regex Use grep to exclude a capturing group
I am using GNU grep with the -P PCRE Regex support for matching strings from a file. The input file has lines containing strings like:
FOO_1BAR.zoo.2.someString:More-RandomString (string here too): 0....
0
votes
1answer
21 views
How can I get all the ENV keys from .rb and .yml files in a directory tree?
I would like to find a way to get the unique keys that are used inside .rb and .yml files. These keys are used inside the hash ENV. So, there are files that contain things like:
ENV['key']
in ...