Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

3 votes
1 answers
328 views
How to do non-greedy multiline capture with recent versions of pcre2grep?
I noticed a difference in behavior between an older `pcre2grep` version (10.22) and a more recent one (10.42), and I am wondering how I can get the old behavior back. Take the following file: ``` aaa bbb XXX ccc ddd eee XXX fff ggg ``` Back with v10.22 (Debian 9), I could achieve non-greedy multi-li...
I noticed a difference in behavior between an older pcre2grep version (10.22) and a more recent one (10.42), and I am wondering how I can get the old behavior back. Take the following file:
aaa
bbb
XXX
ccc
ddd
eee
XXX
fff
ggg
Back with v10.22 (Debian 9), I could achieve non-greedy multi-line captures:
$ pcre2grep --version
pcre2grep version 10.22 2016-07-29

$ pcre2grep -nM '(.|\n)*?XXX' file
1:aaa
bbb
XXX
4:ccc
ddd
eee
XXX
Notice how it captured two multi-line groups, one starting at line 1 (1:aaa), and a second starting at line 4 (4:ccc). Now, with a more recent version (10.42, Debian 12), its behaviour changed:
$ pcre2grep --version
pcre2grep version 10.42 2022-12-11

$ pcre2grep -nM '(.|\n)*?XXX' file
1:aaa
bbb
XXX
ccc
ddd
eee
XXX
Now I only have one group, starting with 1:aaa. Basically, it seems to ignore the non-greedy operator (?). The result is the same if I omit it:
$ pcre2grep -nM '(.|\n)*XXX' file
1:aaa
bbb
XXX
ccc
ddd
eee
XXX
How can I get the behavior of v10.22 back ? In other words, how can I do non-greedy multiline captures in recent versions of pcre2grep?
ChennyStar (1969 rep)
Jul 27, 2025, 12:42 PM • Last activity: Jul 29, 2025, 02:43 PM
1 votes
1 answers
44 views
sudoers for passwordless sudoedit, except for sudoers
I want to configure passwordless sudoedit, to edit files under `/etc` with the exception of sudoers files themselves. I tried using the following `/etc/sudoers.d/sudoedit`: ``` %sudo ALL=(root) NOPASSWD: sudoedit ^/etc/(?!sudo).*$ ``` but that does not seem to work as sudo uses an old regex engine w...
I want to configure passwordless sudoedit, to edit files under /etc with the exception of sudoers files themselves. I tried using the following /etc/sudoers.d/sudoedit:
%sudo ALL=(root) NOPASSWD: sudoedit ^/etc/(?!sudo).*$
but that does not seem to work as sudo uses an old regex engine without the negative lookahead feature. Is there another way to achieve this? Maybe with the combination of /etc and the negation of !/etc/sudo?
thiago (133 rep)
Nov 28, 2024, 06:46 PM • Last activity: Nov 29, 2024, 09:18 PM
0 votes
2 answers
147 views
Unable to grep foreign language in shell script
I am a newbie in shell scripting, I have a text which contain text in following format:- "some foreign language",'corresponding ID to text' for example:- "Назад",IDC_SSB_DLG_BACK_BTN I need to find the text related to ID and save in in text file. Here my sample script:- #!/bin/sh target_file=$1 outp...
I am a newbie in shell scripting, I have a text which contain text in following format:- "some foreign language",'corresponding ID to text' for example:- "Назад",IDC_SSB_DLG_BACK_BTN I need to find the text related to ID and save in in text file. Here my sample script:- #!/bin/sh target_file=$1 output=$2 translationID=IDC_SSB_DLG_BACK_BTN translation=$(cat $target_file | grep $translationID) translationValue=$(echo "$translation" | awk -F',' '{print $1}') translationValueFinal=$(echo "$translationValue" | tr -d '"') echo "$translationValueFinal" >> $output while running this script I am getting error :-grep: (standard input): binary file matches Please suggest a way to grep and save a foriegn language in shell script. Thanks
tabish (510 rep)
Nov 7, 2023, 08:37 AM • Last activity: Nov 7, 2023, 04:52 PM
0 votes
0 answers
187 views
Using pcregrep to search for exact matches of multiline string
I am trying to search a directory using `pcregrep`. I want to search using a long, multi-line string. Basically, I am trying to look through multiple code bases for plagiarism. So I want to be able copy/paste a code block from some code, and then search a directory for any exact matches. The problem...
I am trying to search a directory using pcregrep. I want to search using a long, multi-line string. Basically, I am trying to look through multiple code bases for plagiarism. So I want to be able copy/paste a code block from some code, and then search a directory for any exact matches. The problem I'm having is that when I use pcregrep with the -M option (pcregrep -M), it appears to treat each line break as a separate pattern. So, when I take a code block that I know is unique to one file, I may still get multiple responses because some individual lines may be used elsewhere. Here is what I am using: pcregrep -FlMr "long, multi-line string" /directory/to/search/ What can I do to make sure that it will only return exact matches?
Lee Morgan (101 rep)
Jun 1, 2023, 02:59 PM • Last activity: Jun 1, 2023, 07:44 PM
0 votes
1 answers
982 views
How to install pcre2-tools on Rocky Linux
I am on RL v9.1. The package seems to be there ([pcre2-tools-10.40-2.el9.x86_64.rpm][1]) but yum doesn't see it: yum install pcre2-tools runner_gitlab-runner 1.4 kB/s | 1.0 kB 00:00 runner_gitlab-runner-source 1.4 kB/s | 951 B 00:00 No match for argument: pcre2-tools Error: Unable to find a match: p...
I am on RL v9.1. The package seems to be there (pcre2-tools-10.40-2.el9.x86_64.rpm ) but yum doesn't see it: yum install pcre2-tools runner_gitlab-runner 1.4 kB/s | 1.0 kB 00:00 runner_gitlab-runner-source 1.4 kB/s | 951 B 00:00 No match for argument: pcre2-tools Error: Unable to find a match: pcre2-tools
msciwoj (371 rep)
May 24, 2023, 01:13 PM • Last activity: May 24, 2023, 01:32 PM
0 votes
1 answers
276 views
Is possible to discard outer brackets in regex and consider inner bracket inside?
I have many patterns with bracket enclosure, I made a regular expression where is not considering brackets and just only what is inside/between them, but exists a problem when the text within brackets contain [] brackets too. Thanks! Regex: `(? (?<=\[).*?\[?(?=\]))` For example, ``` A) [ClusterRecei...
I have many patterns with bracket enclosure, I made a regular expression where is not considering brackets and just only what is inside/between them, but exists a problem when the text within brackets contain [] brackets too. Thanks! Regex: (?(?<=\[).*?\[?(?=\])) For example,
A)   [ClusterReceiver            ]
B)   [first-second-third-8050-exec-a       ]
From above, B) is working perfectly, but A) not What is being returned for every case (without quotes): ---
B) "first-second-third-8050-exec-a "
A) "ClusterReceiver[99"
What is desired? ------------------
B) "first-second-third-8050-exec-a "
A) "ClusterReceiver"
The problem is when exist [ ] bracket enclosure within outer [ ] enclosure. The worst case is when exists that problem like A), can you help me by giving a suggestion how to accept at least 1 bracket, in order to have A) as desired "ClusterReceiver" ?
dcubaz (23 rep)
Nov 16, 2022, 08:14 PM • Last activity: Nov 16, 2022, 08:27 PM
2 votes
1 answers
548 views
Capture output of a command that get stuck
I am trying to use regex for capturing some text pattern in an ouput of a command and create an array `list_mqs`. The problem is that this previous command get stuck and never terminates. So, when I use a `pipeline | egrep`, never starts the `egrep` command because the other routine did not finish....
I am trying to use regex for capturing some text pattern in an ouput of a command and create an array list_mqs. The problem is that this previous command get stuck and never terminates. So, when I use a pipeline | egrep, never starts the egrep command because the other routine did not finish. The patterns that I am trying to capture are: MQ_1A, MQ_F5,MQ_H7.
[user@host] list_mqs=($(command_never_ends | egrep -io "MQ_*"))
Is there any way that I can redirect the output of the previous commands even it never ends(or maybe set a counter and if reached exit), and pass those content to be analyzed by the egrep routine?
dcubaz (23 rep)
Oct 19, 2022, 07:57 PM • Last activity: Oct 19, 2022, 08:59 PM
0 votes
2 answers
56 views
GREP PCRE RegExp: match desired line, then match the number, then remove the commas to match digits only
With the help of [https://regexr.com/][1] I'm trying to understand and learn `grep -P`. So far I managed to get this result: ``` $ cat Dogtooth\ \[2001\,\ Lanthimos\ Yorgos\].mp4.info Filename: Dogtooth [2001, Lanthimos Yorgos].mp4 Title: Κυνόδοντας File size: 4,240,762,886 bytes Video duration: 5,8...
With the help of https://regexr.com/ I'm trying to understand and learn grep -P. So far I managed to get this result:
$ cat Dogtooth\ \[2001\,\ Lanthimos\ Yorgos\].mp4.info
Filename:               Dogtooth [2001, Lanthimos Yorgos].mp4
Title:                  Κυνόδοντας
File size:              4,240,762,886 bytes
Video duration:         5,839 seconds
I'm successful in getting the value of *Filename* with ^F\w+\:\s+\K.+\.\w{1,4}$
$ grep -o -P '^F\w+\:\s+\K.+\.\w{1,4}$' Dogtooth\ \[2001\,\ Lanthimos\ Yorgos\].mp4.info
outputs to Dogtooth [2001, Lanthimos Yorgos].mp4 Yay!! :D
But now comes the headache... I can't get value of *File size* Original string: - File size: 4,240,762,886 bytes Expected result: - 4240762886
I only managed to get this far (matching the digits): (^.*size\:\s+\b)\K(\d.+\d) But I still miss how to get get rid of those damn *commas*...
T. Caio (129 rep)
Aug 25, 2022, 06:12 PM • Last activity: Aug 25, 2022, 11:17 PM
6 votes
3 answers
7393 views
How to grep for repeated patterns of repeated patterns?
I have a text file, fred.txt: ``` % cat -e fred.txt 00:$ 00:04:$ 01:00:23:34$ 01:$ 01:40:$ 01:40:32:$ % ``` I can grep for a line with 2 digits and a colon: ``` % pcregrep -e '[\d]{2}:' fred.txt 00: 00:04: 01:00:23:34 01: 01:40: 01:40:32: % ``` but when I try to get repeating patterns of that patter...
I have a text file, fred.txt:
% cat -e fred.txt 
00:$
00:04:$
01:00:23:34$
01:$
01:40:$
01:40:32:$
%
I can grep for a line with 2 digits and a colon:
% pcregrep -e '[\d]{2}:' fred.txt   
00:
00:04:
01:00:23:34
01:
01:40:
01:40:32:
%
but when I try to get repeating patterns of that pattern, it doesn't find them:
% pcregrep -e '[[\d]{2}:]{2}' fred.txt
%
I'm looking to get the same output as this:
% pcregrep -e '[\d]{2}:[\d]{2}:' fred.txt
00:04:
01:00:23:34
01:40:
01:40:32:
%
Eventually I'll be looking for more nested repeating patterns in a larger file so I don't want to define each time the pattern repeats. How do I grep for the lines that have that pattern repeating?
Johnny Rollerfeet (155 rep)
Feb 2, 2022, 06:26 PM • Last activity: Feb 2, 2022, 09:07 PM
1 votes
0 answers
827 views
Pcre2grep multiline regular expression
I'm trying to create a regular expression to match the first html of a text file with this content: garbage garbage &#167; test &#167; garbage garbage &#167; test &#167; garbage garbage &#167; test &#167; garbage garbage first I tried something similar on regex101.com with PCRE2 and single line mode...
I'm trying to create a regular expression to match the first html of a text file with this content: garbage garbage §
test
§ garbage garbage §
test
§ garbage garbage §
test
§ garbage garbage first I tried something similar on regex101.com with PCRE2 and single line mode and it works ok. Then I tried it on Windows with Notepad++ with "dot matches new line" option enabled, it works ok there too. Now I'm trying to do the same on Arch Linux with pcre2grep, i've tried with this regular expression: pcre2grep -o '(?s)' test.txt but it doesn't work and I don't know why because from what i have understood the "(?s)" modifier should enable the PCRE2_DOTALL. How can i create an equivalent regular expression with pcre2grep ?
debe80 (11 rep)
Oct 17, 2021, 01:46 PM
1 votes
1 answers
624 views
Does pcregrep (or pcre2grep) have the equivalent of grep's --max-count option?
Take the following file : ``` aaa bbb XXX ccc ddd eee XXX fff ggg ``` I'd like to print, **with `pcregrep`**, all lines until the first one containing `XXX` : ``` aaa bbb XXX ``` Is it possible (with `pcregrep` or `pcre2grep`) ? I achieved this : ``` $ pcre2grep -nM '(.|\n)*?XXX' file 1:aaa bbb XXX...
Take the following file :
aaa
bbb
XXX
ccc
ddd
eee
XXX
fff
ggg
I'd like to print, **with pcregrep**, all lines until the first one containing XXX :
aaa
bbb
XXX
Is it possible (with pcregrep or pcre2grep) ? I achieved this :
$ pcre2grep -nM '(.|\n)*?XXX' file
1:aaa
bbb
XXX
4:ccc
ddd
eee
XXX
Is there a way to quit after the first match ? It seems that pcre2grep doesn't have a --max-count option like grep has. Nota : I'm aware that there are better tools to get the desired result (sed, among others), but I'm not looking for alternate solutions here. My question is about pcregrep or pcre2grep, I'd like to know if there is a way to emulate grep's --max-count option.
ChennyStar (1969 rep)
Oct 16, 2021, 03:05 PM • Last activity: Oct 17, 2021, 01:34 PM
0 votes
2 answers
58 views
How to replace duplicated multi-line groups, e.g. from git & diff3
I'm rebasing legacy code, and finding lots of conflicts caused by scripts -- usually code formatters. The changes are simple and predictable, so I can trivially re-run the script to apply the changes to the code -- which usually leaves me with three identical "snippets" surrounded by git's diff mark...
I'm rebasing legacy code, and finding lots of conflicts caused by scripts -- usually code formatters. The changes are simple and predictable, so I can trivially re-run the script to apply the changes to the code -- which usually leaves me with three identical "snippets" surrounded by git's diff markers. How can I write a script to find three matching snippets, with their diff markers, and convert them to a single snippet? Example: git rebase produces this:
>>>>>> 0cfd85b8e3... Beautify.
Re-running the formatter gives this:
>>>>>> 0cfd85b8e3... Beautify.
which I would like to convert to this:
ACC
    BCC
I've tried many, many things including grep (it struggles with new-lines) and pcregrep (I struggle with back-references/capture-groups). Any ideas?
Steve Almond (176 rep)
Mar 28, 2021, 10:01 AM • Last activity: Aug 20, 2021, 12:24 AM
-1 votes
1 answers
255 views
What are the difference between `\b` vs `\S` in PCRE grep?
I know now that `\b` is for `Word Boundaries` while `\s` and `\S` is for `Shorthand Character Classes` where `\s` (lowercase) is for whitespace (space, tab, vtab, newline) and `\S` (uppercase) is the other way around. Anything but whitespace wolf@linux:~$ cat space.txt 0space 1 spaces 2 spaces 3 spa...
I know now that \b is for Word Boundaries while \s and \S is for Shorthand Character Classes where \s (lowercase) is for whitespace (space, tab, vtab, newline) and \S (uppercase) is the other way around. Anything but whitespace wolf@linux:~$ cat space.txt 0space 1 spaces 2 spaces 3 spaces 4 spaces wolf@linux:~$ I don't see any difference in these 2 commands. wolf@linux:~$ grep -P '\S\s\S' space.txt 1 spaces wolf@linux:~$ grep -P '\S\s\S\S' space.txt 1 spaces wolf@linux:~$ vs wolf@linux:~$ grep -P '\b\s\b' space.txt 1 spaces wolf@linux:~$ grep -P '\b\s\b\b' space.txt 1 spaces wolf@linux:~$ Reference: https://staff.washington.edu/weller/grep.html
Wolf (1741 rep)
Oct 9, 2020, 07:48 AM • Last activity: Oct 9, 2020, 09:35 AM
0 votes
1 answers
91 views
last match with RegEx
I do `grep -oP '(?<=%28).*?(?=%29)' f.txt` on the following line chr01_pilon_pilon gmap-gsnap mRNA 55846944 55847057 . + . ID=NbD052382.1.mrna1;Name=NbD052382.1;Note=cytochrome b6/f complex subunit V %28plastid%29 %28NP_054520.1%29;Parent=NbD052382.1.path1;coverage=100.0;identity=100.0;indels=0;matc...
I do grep -oP '(?<=%28).*?(?=%29)' f.txt on the following line chr01_pilon_pilon gmap-gsnap mRNA 55846944 55847057 . + . ID=NbD052382.1.mrna1;Name=NbD052382.1;Note=cytochrome b6/f complex subunit V %28plastid%29 %28NP_054520.1%29;Parent=NbD052382.1.path1;coverage=100.0;identity=100.0;indels=0;matches=114;mismatches=0;unknowns=0 and I get plastid%29 %28NP_054520.1%29. How is it possible to get the last match only NP_054520.1? Thank you in advance
user977828 (913 rep)
Sep 30, 2020, 11:08 PM • Last activity: Sep 30, 2020, 11:16 PM
2 votes
3 answers
4137 views
Grep Lines from File with spaces
I have File which contains Records ``` qwe.google.com IN A 1.1.1.1 qwe.google.com IN {uneven-space} A{space}1.1.1.1 qwe.google.com IN CNAME asd.google.com ``` I need to grep all the lines which contains `IN A`, as it contains uneven spaces in between i am not able to do so.
I have File which contains Records
qwe.google.com IN A 1.1.1.1
qwe.google.com   IN {uneven-space} A{space}1.1.1.1
qwe.google.com  IN     CNAME      asd.google.com
I need to grep all the lines which contains IN A, as it contains uneven spaces in between i am not able to do so.
user416391 (43 rep)
Jun 9, 2020, 01:20 PM • Last activity: Jun 10, 2020, 08:27 AM
0 votes
3 answers
649 views
awk and egrep for regular expression
I am very new to unix! trying to figure out, from a fastq file how many reads have 3 or MORE As in a row? I used egrep 'A{3}' to tell me how many AAA I have. But now I want to know >= 3 AAA in a row. But >= doesn't work. Can I use awk to help me determine this? Also, how can I use regular expression...
I am very new to unix! trying to figure out, from a fastq file how many reads have 3 or MORE As in a row? I used egrep 'A{3}' to tell me how many AAA I have. But now I want to know >= 3 AAA in a row. But >= doesn't work. Can I use awk to help me determine this? Also, how can I use regular expression to determine How many reads have a run of 4 or more As followed by something other than a T? (G C or A) So A has to be >= 4, and followed by GCorA EDIT: When I mean to say 3As in a row, I mean something like this: GGCTAAAAAACGGAT
Sarah (1 rep)
Mar 25, 2020, 03:41 AM • Last activity: Mar 26, 2020, 12:48 AM
0 votes
1 answers
3728 views
pcregrep missing from CentOS 7
I have a fresh install of CentOS 8, and the pcre package is already installed. However, the pcregrep command is not available and nowhere to be found on the filesystem. According to CentOS docs this should be in the 'pcre' package. (See [centos.pkgs.org][1] ) > dnf provides */pcregrep > > dnf provid...
I have a fresh install of CentOS 8, and the pcre package is already installed. However, the pcregrep command is not available and nowhere to be found on the filesystem. According to CentOS docs this should be in the 'pcre' package. (See centos.pkgs.org ) > dnf provides */pcregrep > > dnf provides */pcre2grep finds nothing. BTW< pcre2 is also installed, but still no pcre2grep command available. Can someone help my find 'pcregrep' command?
TSG (1983 rep)
Mar 7, 2020, 05:05 PM • Last activity: Mar 7, 2020, 07:26 PM
1 votes
3 answers
917 views
How to replace a line in text using RegEX?
The problem I have is that is trying to match both sets of delimiter (above and below) **I'm trying to match only the second part of the delimiter below (bolded).** [![enter image description here][1]][1] **This is so I can add a new version made on the same day to multiple files. Using perl, so I c...
The problem I have is that is trying to match both sets of delimiter (above and below) **I'm trying to match only the second part of the delimiter below (bolded).** enter image description here **This is so I can add a new version made on the same day to multiple files. Using perl, so I can get a result like this when I make the replacement** enter image description here **How ever according to https://regex101.com/ (and my experience when I ran the command) it selects both sets of delimiters,** enter image description here **making a replacement above and below.** enter image description here This is the RegEx I'm using (?!V[0-9]{2}.[0-9]{2}.[0-9]{4}.1)(.*=.$) And the comand in UNIX: perl -pe 's#(?!V[0-9]{2}.[0-9]{2}.[0-9]{4}.1)(.*=.$)#-* V02.11.2020.1 11/Feb/2020 Author2 Minor Changed Include lms \n -* ================ ============= ==================== =========== ========================================================/#g' path/to/file Is there a way to select the one below? Or the problem originates from the Negative Lookahead? -********************************************************************** EDIT I used the command selected by bey0nd 3,$s/ -\* =[=[:space:]]*\// -* V02.11.2020.1\t 11\/Feb\/2020\t Author2\t\t Minor\tChange include 1ms\n\0/1 It helped a lot with readability But I'm still getting both delimeters (= signs) repalced. I thought that the lookaround function of regex would've helped enter image description here **I'm using perl 5 and sed 4.2** At least I got it to work in regex101.com, but in my version didn't work Hope someone finds it useful (-\* =[=[:space:]]*\/)(?!\n.-\*[[:space:]].V[0-9]{2}.[0-9]{2}.[0-9]{4}.1*)
Javier Vazquez (11 rep)
Feb 12, 2020, 12:55 AM • Last activity: Feb 13, 2020, 11:46 AM
0 votes
1 answers
683 views
How to grep Cisco interface name and all IP Addresses under that section
This is sample of Cisco config file which I get from https://resources.intenseschool.com/mpls-basic-configuration/ and modified it a little bit to make it relevent to this question. I saved it to `file.txt` and trying to `interface` name and all IP addresses under that section using egrep. **`file.t...
This is sample of Cisco config file which I get from https://resources.intenseschool.com/mpls-basic-configuration/ and modified it a little bit to make it relevent to this question. I saved it to file.txt and trying to interface name and all IP addresses under that section using egrep. **file.txt** interface Loopback0 description ** test ** ip address 10.1.1.11 255.255.255.255 ! interface FastEthernet0/0 description ** Connection to ABC ** bandwidth 3000 ip address 10.0.12.1 255.255.255.0 mpls ip ! interface FastEthernet0/1 ip flow monitor all-traffic-monitor output ip address 10.0.100.1 255.255.255.0 ! interface Ethernet1/0 description ** Connection to XYZ ** ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 ! router ospf 1 network 1.1.1.1 0.0.0.0 area 0 network 10.0.12.0 0.0.0.255 area 0 network 10.0.13.0 0.0.0.255 area 0 ! router bgp 14 neighbor 10.4.4.4 remote-as 14 neighbor 10.4.4.4 update-source Loopback0 network 10.0.100.0 mask 255.255.255.0 redistribute static no auto-summary ! ip route 192.168.100.0 255.255.255.0 10.0.100.10 ! **Desired Output** interface Loopback0 ip address 10.1.1.11 255.255.255.255 interface FastEthernet0/0 ip address 10.0.12.1 255.255.255.0 interface FastEthernet0/1 ip address 10.0.100.1 255.255.255.0 interface Ethernet1/0 ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 Here are a few attempts but none of them producing the output that I wanted. **Attempt 1** Syntax: egrep ^interface file.txt Problem: No IP captured [user@linux ~]$ egrep ^interface file.txt interface Loopback0 interface FastEthernet0/0 interface FastEthernet0/1 interface Ethernet1/0 [user@linux ~]$ **Attempt 2** Syntax: egrep -A2 ^interface file.txt Problem: Too much data, I just want interface line and all IP addresses under that section [user@linux ~]$ egrep -A2 ^interface file.txt interface Loopback0 description ** test ** ip address 10.1.1.11 255.255.255.255 -- interface FastEthernet0/0 description ** Connection to ABC ** bandwidth 3000 -- interface FastEthernet0/1 ip flow monitor all-traffic-monitor output ip address 10.0.100.1 255.255.255.0 -- interface Ethernet1/0 description ** Connection to XYZ ** ip address 10.0.13.1 255.255.255.0 [user@linux ~]$ **Attempt 3** Syntax: egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt Problem: Print IP Address only, I want interface name as well [user@linux ~]$ egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt ip address 10.1.1.11 255.255.255.255 ip address 10.0.12.1 255.255.255.0 ip address 10.0.100.1 255.255.255.0 ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 network 1.1.1.1 0.0.0.0 area 0 network 10.0.12.0 0.0.0.255 area 0 network 10.0.13.0 0.0.0.255 area 0 neighbor 10.4.4.4 remote-as 14 neighbor 10.4.4.4 update-source Loopback0 network 10.0.100.0 mask 255.255.255.0 ip route 192.168.100.0 255.255.255.0 10.0.100.10 [user@linux ~]$ **Attempt 4** Syntax: egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt Problem: Almost work, but too much data, I just want interface line and all IP addresses under that section [user@linux ~]$ egrep '^interface|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt interface Loopback0 ip address 10.1.1.11 255.255.255.255 interface FastEthernet0/0 ip address 10.0.12.1 255.255.255.0 interface FastEthernet0/1 ip address 10.0.100.1 255.255.255.0 interface Ethernet1/0 ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 network 1.1.1.1 0.0.0.0 area 0 network 10.0.12.0 0.0.0.255 area 0 network 10.0.13.0 0.0.0.255 area 0 neighbor 10.4.4.4 remote-as 14 neighbor 10.4.4.4 update-source Loopback0 network 10.0.100.0 mask 255.255.255.0 ip route 192.168.100.0 255.255.255.0 10.0.100.10 [user@linux ~]$ If there are better tools to use besides egrep, please let me know.
user11392987 (159 rep)
Jan 6, 2020, 08:08 AM • Last activity: Jan 6, 2020, 06:01 PM
0 votes
1 answers
2081 views
How does pcregrep works?
I have a file: ``` This error -this -this -that [text I want] This error -asd -asfag -adsfhs [text I want] [text I want] This error -asdgsda -asdgg -gasdg ``` I want to match to match except `This error and next three lines`, I can use: pcregrep -vM 'This error\n.*\n.*\n' file It gives output: ``` [...
I have a file:
This error
-this
-this
-that
[text I want]
This error
-asd
-asfag
-adsfhs
[text I want]
[text I want]
This error
-asdgsda
-asdgg
-gasdg
I want to match to match except This error and next three lines, I can use: pcregrep -vM 'This error\n.*\n.*\n' file It gives output:
[text I want]
[text I want]
[text I want]
However, instead of removing them if I want to match them using the same command(by removing v): pcregrep -M 'This error\n.*\n.*\n' file It gives output:
This error
-this
-this
This error
-asd
-asfag
This error
-asdgsda
-asdgg
So how does pcregrep actually works?
Prvt_Yadav (6032 rep)
Jul 16, 2019, 05:01 AM • Last activity: Jul 16, 2019, 06:26 AM
Showing page 1 of 20 total questions