Regular expressions are a means of matching a pattern of characters within a string.

learn more… | top users | synonyms (1)

0
votes
3answers
21 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 ...
2
votes
2answers
426 views

Using regular experision in “rpl” command

I need to replace text in lots of files. I'm using rpl for this purpose, but I can't find any way to make it use regular expressions. Can rpl use "regex"? how? Edit: I'm using rpl 1.5.2 from Ubuntu ...
5
votes
4answers
5k views

How to reduce the greed of a regular expression in AWK?

I have an example: echo "@article{gjn2010jucs, Author = {Grzegorz J. Nalepa}, " | awk '{ sub(/@.*,/,""); print }' Is it possible to write a regular expression that selects the shorter pattern? ...
4
votes
2answers
220 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 ...
3
votes
2answers
1k 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 ...
6
votes
4answers
198 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
25 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
57 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
34 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 ...
4
votes
6answers
200 views

Append something to each list in a file

I have a file, lists.txt, that looks like this: // stuff at beginning of file var list1 = new Array(); i = 0; list1[i++] = 'a'; list1[i++] = 'b'; ... list1[i++] = 'z'; var list2 = new Array(); i = ...
1
vote
2answers
49 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
27 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
21 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
45 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 [ ...
10
votes
2answers
955 views

grep: display filename once, then display context with line numbers

Our source code has error codes scattered throughout. Finding them is easy with grep, but I'd like a bash function find_code that I can execute (eg. find_code ####) which will provide output along ...
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. ...
4
votes
2answers
95 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
36 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 = ...
1
vote
2answers
470 views

Regex : all greek letters with 5 unique letters (meaning that each letter only appears once)

Another one I can't seem to solve : all greek letters with 5 unique letters (meaning that each letter only appears once). my solution : egrep '(.)([^/1])([^/1/2])([^/1/2/3])([^/1/2/3/4])' greek.txt ...
1
vote
3answers
39 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 ...
0
votes
1answer
58 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 ...
3
votes
1answer
41 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]*
2
votes
5answers
942 views

remove leading string in bash

I try getting this sed command to work. I have a string like rev00000010 and I only want the last number, 10 in that case. I have tried this: TEST='rev00000010' echo "$TEST" | sed ...
1
vote
1answer
53 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
35 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 = ...
12
votes
3answers
4k views

Online grep exercises?

Are there any online grep tutorials that would feature some online tool for performing exercises? For example, having some predefined text sample and being told what patterns to find and so forth? It ...
3
votes
3answers
7k views

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs ...
17
votes
11answers
4k 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 ...
2
votes
1answer
59 views

How to use regex inside exec with find?

Is it possible to use regular expressions based on the result (file name) inside the exec argument with find? I want to be able to "exec" based on parts of the argument, like: find . -name pattern ...
1
vote
1answer
43 views

grep also returning non matching lines

I'm trying to grep the output of strings and only find lines that start with a certain number of = chars. I tried strings data.txt | grep ^=* Sample output: 1~{, WX2 };K,< ========== ...
1
vote
2answers
51 views

Grep and ignoring leading whitespace

I've been working on a bash script to search for names of defines, and then grab the hex values for them and put them in the list. Once I have the list of names I'll attempt to search for "#define ...
3
votes
2answers
664 views

Parsing through log file and printing out regex backreferences

I want to parse my /var/log/maillog to print out the email addresses in from=<> and to=<> (mainly to do a quick check for false positives in the DNS RBLs that I've configured) A maillog ...
4
votes
6answers
375 views

Grep the text which matches with a pattern

I was looking for a command which will print the selected portion of a line which matches the conditions. Eg, my text line is like this: p1=X||p2=Y||p3=X||p4=X||p5=X||p6=Y||p7=X I want to print ...
0
votes
1answer
35 views

sed - find string made of 4 numbers with a preceding character and append character

I'm trying to build a sed command that will allow me to search and replace some values in a large csv file. I have this column which contains years, like 2005 - 2006 2001 - 2003 and so on, but I also ...
1
vote
0answers
36 views

LFTP exclude file extensions

I am trying to mirror directories with lftp but I don't want to download filetypes that are notoriously large like .mp4 and .swf. But I am having trouble with the regex - and seeming like the ...
0
votes
2answers
80 views

get values which only ocurre once in the vector

there is a vector of numbers I have which ascend (it is called t8_) 2 7 8 9 11 15 34 91 91 92 94 the problem is that some number happen two times - I don't want to remove them but they have to ...
1
vote
1answer
23 views

Locate --regex with negative lookahead

Problem I tried to implement a negative lookahead when searching a file with locate like this: locate --regex "apple(?!t)" However I'm getting the following error, because there seems to be some ...
1
vote
1answer
75 views

how to replace a sed expression (change and print only matched line) with a perl expression?

I am trying to move to perl (from sed). But how can I replace this sed expression with perl in a similarly short way? sed -n '/patternmatch/s%stuff%changed%p' file The only way I could do it was ...
2
votes
4answers
283 views

Replace regex match with string containing match

I have a file with text as Afghanistan=+93 Albania=+355 Algeria=+213 American Samoa=+1 Andorra=+376 Angola=+244 It has all the country list and its dialing code. I want to replace: ...
1
vote
3answers
72 views

Match beginning of a line with something there?

I want to match the beginning of lines where the line contains something. Command in Perl/Sed 's/^.?/\section{}/g' but the problem here is that it replaces also the first character of sentences. ...
2
votes
5answers
295 views

Duplicate lines and make them unique?

I have the following structure of text lines: 3923 001 L05 LV,L05 RM 3923 002 L12 RA,L12 LA 3923 003 I06 ALL 3923 004 G04 RV,Z09 ALL but i would need this: 3923 001 L05 LV 3923 001 ...
1
vote
2answers
101 views

how to replace a sed/perl expression (change and print only matched line) with a python expression?

I have asked this question to learn how perl could replace sed. Now I want to know how the following commands (which do the same thing) would look like for python: sed -n ...
2
votes
2answers
94 views

Does [[:space:]] work for sed patterns on all Linux distros?

Using [[:space:]] looks a little non-elegant and, quite frankly, Microsoft-ish for sed match patters. I was trying to extract "Last" out of "First Last" and either of the following worked: echo ...
1
vote
1answer
76 views

Pattern matching inside awk

I intend to modify the output of ls -la to display the output of file command. The below command works well for the purpose find /opt -type f | xargs ls -let | awk 'BEGIN { OFS="\t" } { sprintf("file ...
80
votes
5answers
195k views

How do I grep for multiple patterns?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and ...
0
votes
2answers
40 views

grep for lines/files that contain exactly X chars

Here's the output of a grep command I ran: [user@localhost] : ~/Documents/challenge $ grep -i -e ".\{32\}" fileA fileB fileA:W0mMhUcRRnG8dcghE4qvk3JA9lGt8nDl fileB:observacion = new Observacion(); ...
0
votes
1answer
25 views

emacs dired regexp mark files beginning with

In Emacs Dired mode, I'd like to mark directories and files whose names begin with Addr but not files that contain Addr inside the name. I'm using RE-Builder to test my regular expressions and am ...
0
votes
2answers
29 views

Delete folders using regex

I would like to delete folders with specified regex, but I could not build a good regex. I have: /root /my-picture-2015 /my-picture-2015-1 /my-picture-2015-2 /my-picture-2015-1 ...
16
votes
6answers
10k views

How can you combine all lines that end with a backslash character?

Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash? For example, given the file: foo bar \ bash \ baz dude \ happy ...
2
votes
1answer
43 views

How do I match the beginning of a line in a manpage?

When I do man zshbuiltins I typically want to search for a command. Suppose I'm looking for info on the alias command. I get a chunk of text like: : [ arg ... ] This command does ...