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

learn more… | top users | synonyms (1)

2
votes
2answers
29 views

vim find and replace around a string

In VIM, if, for example, I have the text http://a.com http://b.com is it possible to find all lines (the whole line) and replace it with something before and after it, such as: <a ...
2
votes
5answers
476 views

ls | grep works doesn't work with ls --color

I'm trying to list all hidden dirs with the following command ls -lhAF1 | grep -E '^d.*[0-9]{2}:[0-9]{2} \.' which works perfectly fine Explanation for Regex: I'm trying to get all rows that ...
-1
votes
2answers
38 views

scp a file to multiple folders

I wanted to scp file.txt to remote box my-remote-vm-1 In my-remote-vm-1 there are two folders conf-1-ver-2,conf-2-ver-2. The integer part in the folder name can go from [0-9]. What would be the best ...
1
vote
2answers
29 views

Postfix: Remove References to Server Software

Every time my postfix servers sent emails at the destination the emails headers include the following: by suppaserver.example.dev (Postfix) with ESMTPSA id 79D412E1A3A How can I remove the ...
1
vote
3answers
25 views

How do I replace this pattern with a newline inside it? [duplicate]

If I have a text file with this content: someline <!--\ file first read on 2015/01/11 And I want to delete <!--\ and everything until what comes after "on", how do I do it? The ...
3
votes
3answers
44 views

backreferences in `rename` regex

I have A LOT of files named "bar_foo.txt" in a directory, and I'd like to rename them to "foo_bar.txt" instead. In sed and vim the regex to to this would be something like ...
-2
votes
0answers
21 views

Complex sed Command for Insert Command [migrated]

I have a bunch of php files which have many insert commands. In each query i want to insert a coloumn variable admin_id = '$admin_id' i.e. if the query is insert into users (ch_id, num_value) values ...
1
vote
1answer
21 views

cshell alias: How to use nested single quotes(') in in cshell alias

I am trying below alias in cshell: alias sll 'ls -l \!* | grep -oE '[^ ]+$' | paste -s | xargs ls -l' For this CSH says, Illegal variable name. If I use \$, alias will be set without any error. ...
1
vote
1answer
32 views

grep content between two starting bracket

Below is file output that I want to access [1] RsyncCommand: 0 Number of files: 18 Number of files transferred: 0 Total file size: 24.01M bytes Total transferred file size: 0 bytes Literal data: 0 ...
8
votes
2answers
57 views

What does [[.ch.]] mean in a regex?

Alternate title: What is a "collating sequence" or "collating element" in a POSIX-compliant regex? I found the exact technical definition in Section 9.3.5 of the POSIX specs, as item #4 in the list, ...
0
votes
2answers
41 views

perl: Replace string in file with double quotes

Following up question from http://unix.stackexchange.com/a/254637/18887 I have the following string in a bash variable This rule forbids throwing string literals or interpolations. While JavaScript ...
2
votes
2answers
47 views

sed to match pattern between matching curly braces

From a pattern such as [string 1]{string 2} I want to extract string 2, the string between the last pair of matching curly braces -- that is delete [string 1] and the open { and close }. My ...
4
votes
2answers
63 views

BSD sed: Replace only the Nth occurrence of a pattern

Using BSD sed; How can I perform the following substitution?: Before: hello hello hello hello hello hello After: hello world hello hello hello hello In other words; how can I replace only the ...
2
votes
1answer
44 views

Regular Expression to match (not x) and y (!x & y)

I received a desktop day-to-day calendar with puzzles. One such puzzle was deciphering a quote where the letters were substituted with symbols. I used some RegExs to find longer words, then used the ...
1
vote
2answers
71 views

Complex sed Command with AND/OR/NOT

I have a bunch of php files which have many select commands. In each query i want to insert a coloumn variable admin_id = '$admin_id' i.e. if the query is select * from users where abc='xyz' and ...
1
vote
3answers
108 views

regex matching and substring extraction

I have these files, manually created by many different people are coming in. Formatting, although following a certain rule, is not uniform. think of these three lines below ...
0
votes
1answer
24 views

Replacing in files with sed using regex

i want to replace a string in many files like "Total:*points" using sed. I simply don't know how to use sed with regex. Below is what i got. Thank you in advance! find ./ -iname "skp-corr-*.txt" ...
0
votes
1answer
33 views

Substitution with Regular Expressions in gedit

Say I want to remove all digits from a text file using gedit 3.10.4 advanced_find 3.6.0 I have selected the Advanced Find/Replace plugin to support regular expressions out of an internet search. ...
0
votes
2answers
30 views

Why doesn't this find command work? Regex & Find variants for .mp3 .wav and .m4a files

2015.10.25 Move on and Be Blessed.wav dummy.m4a 2015.11.17 BS Full.wav.mp3 2015.11.17 BS MOL.wav.mp3 find . -name '*.mp3' -o -name '*.wav' -o -name ...
1
vote
4answers
47 views

bash extract a substring from “1.2.3-SNAPSHOT”

How can I substring a 1.2.3 from a 1.2.3-SNAPSHOT from bash? I tried echo '1.2.3-SNAPSHOT' | grep -o "^.*(?=(\-SNAPSHOT$))" but it didn't work Ideally I'd like a command to return 1.2.3 in both ...
2
votes
1answer
44 views

Extract URL from non-formatted text

I've found only examples on extracting substrings from formatted text like HTML files, but in my case I need to output a list of URLs for example: ... https://twitter.com/user1/status/xyza ...
1
vote
2answers
271 views

Any non-whitespace regular expression

Im trying to match a string agains a regular expression inside an if statement on bash. Code below: var='big' If [[ $var =~ ^b\S+[a-z]$ ]]; then echo $var else echo 'none' fi Match should be a ...
3
votes
4answers
62 views

Bulk data generation

I need to generate nearly 1 Billion records of unique integers. I tried with awk but it is not generating more than 5million records. Please help. Below is what I had tried so far - awk -v ...
1
vote
2answers
24 views

sed to match zero or more number of spaces in a string

function getVal { sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/' finalSedPattern=${sedPattern/keyVal/$2} echo $(sed $finalSedPattern <<< $1) } This is my Json parser written using ...
4
votes
1answer
113 views

In POSIX sed, does the period (dot) match a newline in a multiline pattern space?

In GNU sed it works. For example, this matches two blank lines in a multiline pattern space (created with N) /^\n$/ Is this standard?
1
vote
2answers
14 views

bash regex to find and save string from file

I'm a noob, so thanks for your assistance. Need some bash help... My file, hi.html, has many lines of text. I need to hunt down the line that starts with Hello abc123 blah blah blah and then save ...
4
votes
1answer
30 views

Bash regular expression pattern matching spaces, within double brackets test, using “~=” operator

Simply said, I just can't find a way to match a variable against some pattern containing spaces. Here is what I expected to work (echo 'ok' string) item='foobar baz' pat=".+bar baz" if [[ "$item" =~ ...
6
votes
2answers
411 views

Why does [0-9]* match where there aren't any digits?

so the command is : echo "abc 123" | sed "s/[0-9]*/h/g" and im getting output as hahbhch h how am i getting this output? the output i expected it to be is abc h which im getting by ...
4
votes
2answers
153 views

Bash regex capture group

I'm trying to match multiple alphanumeric values (this number could vary) from a string and save them to a bash capture group array. However, I'm only getting the first match: mystring1='<link ...
6
votes
2answers
175 views

Match the next pattern which is less than the previous match

I've got a file with QR-code lines and I want to grep for only those whose subsequences do not increase in length. Example: This one is good because the next sequence is less or same as previous: ...
1
vote
3answers
192 views

How to match two lines at once in vim

I have a file that contains records with tab separated format but in two lines, I want to arrange records in one line with tab separated values using vim editor pattern match. Ex. Abc PQR ...
0
votes
2answers
35 views

Parameter expansion in grep

To retrieve strings of the following pattern from a text file populated with multiple entries: entry=G_1234 entry=G_5678 entry=G_2345 The following grep statement doesn't provide any result grep ...
4
votes
3answers
167 views

How to replace text in a column

I have a huge file (more than 2 GB) where data is like below. 12,324,32342,E:fsdsf,23432,34534,45345,324 13,3224,342,E:werwefsdsf,23432,34534,45345,324 121,3244,33442,E:,23432,34534,45345,324 ...
3
votes
2answers
57 views

Replace only on the first matching line with sed

Using BSD sed (no GNU extensions), how can I perform an operation similar to the example provided below, but where instead of the line number, the replacement is performed on the first line in which a ...
5
votes
3answers
236 views

egrep regular expression - same word in the beginning and end

I want to find all the lines that have the same word in the beginning and in the end of the line. For example: goodword fgdlakj 3t sfkl 43lk fkl goodword bad sfa;lk3t dgk;gs 34;kl ...
5
votes
3answers
165 views

OR in `expr match`

I'm confused as to why this does not match: expr match Unauthenticated123 '^(Unauthenticated|Authenticated).*' it outputs 0.
2
votes
3answers
47 views

How to extract particular field from rows from a file, based on matching subsequent rows

I am trying to replicate a workplace problem. I have a xml file like below [~]$ less -N sample.xml 1 <SOURCE BUSINESSNAME ="" NAME ="TABLE1" FOO="ABCD"..... > 2 <SOURCEFIELD ...
0
votes
4answers
40 views

How to remove a first character of that particular sentences from a list of output [duplicate]

grep /var/log /etc/rsyslog.conf | awk '{print $2}' Output: /var/log/messages /var/log/secure -/var/log/maillog /var/log/cron -/var/log/spooler /var/log/boot.log I want the result to be: ...
14
votes
1answer
712 views

How to “grep” for line length *not* in a given range?

NOTE: This question is the complement of this Q&A: How to "grep" for line length in a given range? How can we grep for lines that have less than 8 or more than 63 characters, none ...
0
votes
1answer
30 views

Regex on multiple files

Very new to regex and have a directory of files that I would like run this regex on but don't know how. help would be great. This is the regex: (?<=#).*
7
votes
5answers
1k views

How to find lines containing more than 100 characters and contains “if”?

So, probably I should use grep for this. As long as I need recursive search I should use grep -r. But then I don't know what should I do next ;) How can I do that?
2
votes
2answers
39 views

Use of \$ at end of variable name passed to grep

I was going through one of the shell scripts and found a line : ls $dir | grep -c "$NAME.${var}\$" I understand that $ would expand the variables NAME and var, but is it required to put \$ at the end? ...
1
vote
1answer
21 views

How do I keep a part of the pattern matched and use it to replace on BSD sed?

I have found a soultion for GNU sed, and I also found something like \1, but the Terminal tells me it's "not defined in RE". But what I want to do is this: If I have a string that looks like this ...
1
vote
3answers
57 views

How to sort blocks of lines with a specific line from the block?

I have a file with the data like this BEGIN hello2 5 world1 END BEGIN hello4 2 world5 END BEGIN hello6 4 END I want to sort the lines in the following manner based on the number inside the block. ...
2
votes
1answer
48 views

Add text between multiple lines with sed if previous line does not match pattern

I'm struggling to add <dl> tags around a definition list with sed, and perhaps there is an easier way to do it (I'd love to know). I would like to search a file for any lines that contain a ...
5
votes
2answers
62 views

sed string with bad chars

I have a random string with ugly chars in it: ÓˇÌ„ˇ™ÌÓ‹ÍÙ› ‹„ı That chars have to be eliminated. The Whitelist consist of: a-zA-Z0-9 -_* + ß ä ü ö () % @ € & = . and Space My first Script ...
0
votes
1answer
77 views

Regular expression not matching all possibilities

I am having a little trouble with a regular expression ... I am trying to parse an HTML file looking for certain text between two <br> tags ... between these <br>(text goes here)<br> ...
1
vote
2answers
18 views

using egrep & regex

Suppose I have the following text file called inputFile: hello HELLO Hello World wOrld asdgfsafd abc 123 1 3 5 a b and I want to print out the lines containing only one word(any sequence with ...
4
votes
2answers
84 views

Find Files not an regex whitelist

I have a huge Folder with thousands of Files. Some Files have some Characters in it, which are not allowed. (UTF-8 signs) So I have a white list of allowed characters and a beginning of a bash-script ...
16
votes
3answers
1k views

Vim - how to replace one new line \n with two \n's

In vim editor, I want to replace a newline character (\n) with two new line characters (\n\n) using vim command mode. Input file content: This is my first line. This is second line. Command that I ...