All Questions

0
votes
1answer
13 views

Extract pattern in perl

Is it possible to have just one regex which can extract the pattern "Mango-1.whatever.." in any of these 3 conditions (i.e. $dummy). I keep printing everything after this pattern. If I use comma in ...
0
votes
3answers
84 views

perl regex - How to match foobar that does not begin with (\s*//\s*)

Basically I want to find foobar on uncommented lines like: // foobar # do not match me foobar # match me otherfoobar # do not match me To match the commented lines I use this: ^(\s*//...
0
votes
2answers
61 views

Linux command to delete this

I want to delete this kind a pattern in my text file [-.(D_)-]. I did with sed and awk but no results. basically i want to delete anything that comes between [- & -] along with these characters ...
1
vote
2answers
92 views

RegEx for replacing capture group content using sed [closed]

Have inputs.conf files in multiple directories that needs to match and parse each stanza and modify the index= to index=secure. This are files type in inputs.conf and also do run the script to locate ...
0
votes
0answers
24 views

Bash-Perl Make Substitution and Return array (string & number of replacements/substitutions)

I have this code that makes substitutions in a file segment. The code should be left mostly unharmed except for this problem. I need to return back to bash, the number of replacements made by perl ...
0
votes
2answers
44 views

Perl Negative Lookbehind with variable length bypass maybe?

I have this simple code perl -we 'my $file= " # parameter=10 # parameter=10 # parameter=10 parameter=10 parameter=10 "; $file=~ s/((?<!# ))\s*parameter\s*=.*/parameter=replaced/g; ...
-2
votes
3answers
50 views

Perl multiline match for 'something' which starts without '#\s'

UPDATE: The REAL answer to this MULTILINE question was given here by Stephane https://unix.stackexchange.com/a/521560/354415 Alternatively go line by line with perl by Terdon below: https://unix....
0
votes
1answer
76 views

Get lines of file matching a regex

Is there a more compact way to get lines of a file that match a regex than the following? open my $fh, '<', $file or die "Cannot open $file: $!"; while ( my $line = <$fh> ) { next ...
0
votes
1answer
56 views

Insert new text in the middle of a file replacing old text without re-inserting the same things over and over?

I have a file that I want to update relatively frequently. What needs to be done is that a specific part of the file would be changed to include some more lines. I am doing it with sed and \a option: ...
5
votes
2answers
505 views

Perl command line regex to modify all pattern matches

I'm trying to use some arithmetic over the matched patterns in perl command line. I'm able to do it for one match but not for all. str="a1b2c3" perl -pe 's/\d+/$&+1/e' <<<"$str" a2b2c3 ...
1
vote
1answer
53 views

Skip text in output with grep in perl mode

I have text like this: blah, blah <foo:ImportantText> blah blah time=1.234 blah blah blah, blah <foo:AlsoImportant> blah blah blah time=9.9 blah blah blah, blah <foo:ImportantText> ...
-2
votes
1answer
50 views

replace name after specific syntax

We have Bash script. We want to change the name - master02 with $machine_master in the Bash script. value=master02_up #master02 http://master02.$domain:8080 How to change master02 to $...
5
votes
2answers
107 views

sed regexp text processing capture grouping referencing alternation confusion

sed's capture grouping ALTERNATION is not working as expected. While alternation has the lowest precedence, here is the confusion: echo "abcd_aefghi" | sed -r "s/(a)(b)(c)(d)|(a)(e)(f)(g)/\8/g;" ...
0
votes
1answer
83 views

Regexp: Horribly slow regexp in perl - how to optimize?

This: echo BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BA BBBBBBBBBBBBBBBBBBBBBBBBBBBBA | perl -pe '/^(.*?B(?:A.*?B){30})A/' takes an astonishing 8 seconds on my machine. I had ...
3
votes
1answer
2k views

Opposite of \K, to keep the stuff right [duplicate]

On the perlre's extended patterns page we can read about \K: Keep the stuff left of the \K, don't include it in $& Here is the practical example using GNU grep (which actually keeps stuff ...
0
votes
2answers
73 views

Delete text between parentheses, but never past empty line

Consider a text file, with lines of text gathered in many blocks, where each block is separated by at least one empty line. Using a Bash one-liner, how do I delete all text from < to either > or ...
0
votes
2answers
120 views

Regex Pattern and Replace line with substitution

For a migration process, I need to do some replacement in my bash script. So in my .txt files, I have these references for example: [[File:My Image.png|400px|thumb|center|My Image]] [[File:My Image....
0
votes
3answers
104 views

what is the best way to match a version number from file

what is the best way to match specific version number ( or version number ) from file I did some options , but I think there are other best approach ( as perl one-liner ) version=23.092.12 cat ...
0
votes
1answer
39 views

How to remove a place holder in a text file with text from another file when multiple lines are present?

I have a file: - id: 1.4.25.2 name: 'Configuring a VNC Server' permalink: '/rhcsa/managing-network-services/configuring-vnc-access/configuring-a-vnc-server' description: '<*...
0
votes
1answer
47 views

Invert last two subfolders of the directory structure

Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question: https://superuser.com/questions/221/how-can-i-invert-a-directory-structure This is ...
0
votes
0answers
109 views

Removing lines from file matching 2 strings

I am trying to remove lines in a file that contain 2 strings. I am using: perl -i.tmp -ne "print unless /$STRING_A/ and /$STRING_B/" /foo/bar/file.txt The result is that all lines from the file ...
3
votes
4answers
3k views

Cut a file delimited on a number with one or more digits

I'm looking for a way to extract the first column of a text file that has no specific delimiters except for arbitrary digits that begin the next column. Example: John Smith 1234 Main Street Amy Brown ...
0
votes
2answers
743 views

Extract variables from text file into array with Bash, Perl and Regex

I would like to extract variables from a text file with Bash, Perl and Regex. The file looks like this (and is already read into the variable $str): Filename: XXXXX Type: XXX Size: XXXX Unimportant ...
0
votes
2answers
107 views

Match a pattern and replace the first instance of string following it (regardless of spaces or tabs)

I asked a similar question a while back but I'm hitting a brick wall getting this one to work. I'm scripting the modification of a config file laid out like this: [Thing1] [[subthingA]] ...
4
votes
3answers
115 views

Character count of language X in mixed text file?

I have mixed-language text files, and would like to count the simple total number of printable characters of one of the languages. It helps that the languages inhabit different unicode ranges. My ...
0
votes
1answer
41 views

perl problem to get desired output

nameFile seq_1014 seq_1039 seq_2848 seq_3213 seq_6847 seq_6980 seq_6997 seq_9319 seq_9561 seq_9850 # outputFile >seq_1014 >seq_1039 >seq_2848 >seq_3213 >seq_6847 >seq_6980 >...
0
votes
1answer
96 views

How do I replace multiple lines near regex characters?

Here is a portion of my source file, called "mysqld": exit $r ;; 'bootstrap') if test "$_use_whatever" == 1 ; then log_failure_msg "Please use galera_new_cluster to start the ...
0
votes
2answers
172 views

Regex conditional

In my code file I have several lines asT V= X T(A); And I want the text after = to be replaced as X(T,A) with some help I got this pattern s/X\s+([^\(]*)\(/X(\1,/g; for find and replace it works ...
3
votes
4answers
2k views

Match a pattern and replace the first instance of string following it

I'm scripting the modification of a config file laid out like this: [purple] auth = no enabled = 0 username = password = priority = 0 host = True [shovel] group = manual = False enabled = 0 ...
0
votes
1answer
554 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)(.*?[\=])(.*?[\...
10
votes
1answer
539 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
54 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
323 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-...
0
votes
1answer
143 views

Perl script extracting web links using regular expression

I am trying to write a script in perl and trying to extract web links using regular expression from a file called file.txt(which has list of website). I am not able to print the links. Here is my ...
4
votes
1answer
459 views

Perl regex get word between a pattern

I have a working perl regex using grep. I am trying to understand how it works. Here is the command command. grep -oP '(?<=location>)[^<]+' testFile1.xml Here are the contents of ...
1
vote
1answer
162 views

How to Do This LookBehind in Regex?

Data: 1.000000000000002, 0.999999999999999 Expected output: 1.0..02, 0.9..9 where you can replace .. with any other shorthand for cut Pseudocode in Matlab float_thing=1.0000000000000002; % 0....
-2
votes
1answer
42 views

comparing the numbers and collecting details and providing output to new file [closed]

file1 contains many numbers like shown below: 345656565465 783329439432 345656565465 783329439432 ....... file2: 345656565465;ram;dfdfdf54532fdf;............................... 783329439432;sam;...
0
votes
2answers
566 views

Doing a find-and-replace between two strings, across multiple lines

Okay, so what I need to do is to convert all comment blocks of this format: /** * */ To: /*! */ But with an additional caveat that the line after the /** cannot contain "Copyright", because we ...
3
votes
3answers
345 views

sed regular expression behaving differently than in vim and perl?

Here's a sample 'test.txt' abcdX1yad45das abcdX2fad45das abcdX3had45das abcdX4wad45das abcdX5mad45das sample desired output: X1yad X2fad X3had X4wad X5mad I could get it working in vim with: :% s/...
1
vote
2answers
88 views

SED Search and Replace on Linux [duplicate]

I want to change the value from 1521 to 1525 in FROM_HOST only in file gexhosts.cat: FROM_HOST db201521 TO_HOST db201521 Is there a way to do that or some work around? I would really prefer ...
0
votes
3answers
76 views

Perform regex search on a previously regex-matched line (one-liner)

I need to print a string which is in a unique line in a long text file. Example relevant line in file long.txt: Software version 4.0.10, 2002-03-28 The wanted string is exactly 4.0.10. The steps ...
5
votes
1answer
147 views

Perl one liner to find words longer than 63 characters

I have a number of XML files containing sanskrit texts to be converted to tex. Latex has a maximum of 63 characters per word for its hyphenation to work, everything longer than that will not be ...
1
vote
2answers
93 views

Why does perl regex find pattern \\label\{.*?\} but not \\ref.*?\} or \\autoref\{.*?\} in TeX Files?

I am using perl to parse many multilingual text files. I need to change the text between two patterns: Desired Changes For example, the English original version looks like this: \label{whatever} \...
0
votes
2answers
1k views

perl: Replace string in file with double quotes

Following up question from https://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
1answer
127 views

Why can't I pass a regex in @ARGV on the command line?

Why can't I use if($l =~ $ARGV[0]) but I can use if($l =~ /$ARGV[0]/g? first case $ perl script.pl '/^[\w]/g' second case $ perl script.pl '^[\w]'
3
votes
2answers
1k views

How to escape metacharacters for egrep like metaquote from Perl?

Perl has a function called metaquote() to escape all special characters for a regular expression. Is there an equivalent technique for egrep? Example: If I am searching for the string abc.def.ghi, I ...
2
votes
2answers
728 views

Replacing matching text after two matching lines

I have a YAML file which includes a stanza looks this: admin::common::passwords: alice: password: '$6$oTQhLvN/4VFJPscD$8LYwUMSFi' bob: password: '$6$JKOtLF0wHeZfIskt$W/M5.ugDS' If the ...
2
votes
3answers
91 views

How to append a line that does not start with '<' to the previous line

I have a log which contains XML lines. Sample format below: <head> <body> <line> asdasd</line> </body> </head> I want to scan the log file and ...
3
votes
2answers
334 views

How to search for the word stored in the hold space with sed?

This is a sed-specific question; I am well aware it could be done with other tools but I am working on expanding my knowledge of sed. How can I use sed to globally quote (actually backtick) a word ...
2
votes
1answer
89 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 the ...