Perl search term [closed] - perl

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write the following statement as a Perl search expression:
Find all occurrences of the word "cat" followed by the word "dog" within 13 characters.
So for example the text "catajdwos dogqwzv" would be a result.
Do someone know how to do this?

I'd use a regular expression evaluated by the match operator. You'd use the g modifier of the match operator to find all occurrences.
while ($str =~ /...pattern.../g) {
...
}
Refer to your class notes on regular expressions to compose the pattern you need.

The following should work...
$str = "sdfcatsdfdogffdfcatsdfjljlfflkfjflkjfdogsfsd";
#arr = $str =~ /(cat).{0,10}dog/sgi;
print join(',', #arr), "\n";
s to match over newlines, g to extract all matched instances, and i to ignore case.
I'm not sure what you mean by 'within 13', but I've assumed here that as many as 10 characters can separate the 't' in cat from the 'd' in dog.

Related

matching negative number in a line not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How to match negative number in a line.
input line :strongest_signal_indication Invalid {-1} some text
I was trying this regex expression:
/ ( [+-]?\d+ )| (\s+ ( \{ [^-]+ \} |\S+)) /xg
but it did not work for me
Any help please
Your pattern actually does match.
$ perl -E'
say
"Invalid {-1}" =~ / ( [+-]?\d+ )| (\s+ ( \{ [^-]+ \} |\S+)) /x
? "match" : "no match";'
match
In fact, / [+-]? \d+ /x would have sufficed.

Storing regex with '^' and '$' inside constant [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I revised my code and realized I stored the regex inside constant and then used the latter's value for the variable
I'm trying to store a regular expression inside variable constant using the qr// operator. Everything is fine except for '^' and '$'. I need them to match beginning-of-line and end-of-line respectively.
use constant REGEX_LINE => qr/\^(\s*)(.*)\$/;
my $rx = REGEX_LINE;
Printing $rx reveals that it contains some addiotional stuff:
(?^:^(\s*)(.*)$)
Of course now the regex doesn't match my data
If you expect ^ and $ to match start and end of line,
don't escape them (or else they will match ^ and $), and
use /m (or else they will match the start and end of the string).
use constant REGEX_LINE => qr/^(\s*)(.*)$/m;
Add escape character(\) before $ symbol otherwise it will consider as a part of variable

Using sed how to remove last character only in the first line [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I use sed to remove the last character from only the first line of a file?
You can for example use this:
sed '1 s/.$//' file
Explanation
1 indicates the line in which we want to perform the action.
given the syntax s/text/replacement/, we look for any character with . followed by $, which indicates end of line. Hence, we look for the last character before end of line and replace it with nothing. That is, we remove the last character of the line.
To edit the file you can use -i.bak.
Test
$ cat a
hello this is some text
and this is something else
$ sed '1 s/.$//' a
hello this is some tex
and this is something else
For fun, let's see how to accomplish this with awk:
awk -v FS= -v OFS= 'NR==1{NF=NF-1}1' file
This sets the input and output field separators (FS, OFS) as empty (same as BEGIN{FS=OFS=""}), so every single character is a field. Based on that, when the record is 1 (in this case, when we are in the 1st line), decrement the number of fields (NF) so that the last character is "lost". Then 1 is a true condition that makes awk perform its default action: {print $0}.

How to double quote all fields in a text file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a quick and efficient way to double quote all fields in tab delimited or comma separated text files.
Ideally, this would be a Perl one-liner that I can run from the command-line, but I'm open to any kind of solution.
Use Text::CSV:
perl -MText::CSV -e'
my $c = Text::CSV->new({always_quote => 1, binary => 1, eol => "\n"}) or die;
$c->print(\*STDOUT, $_) while $_ = $c->getline(\*ARGV)' <<'END'
foo,bar, baz qux,quux
apple,"orange",spam, eggs
END
Output:
"foo","bar"," baz qux","quux"
"apple","orange","spam"," eggs"
The always_quote option is the important one here.
If your file does not contain any double quoted strings containing the delimiter, you can use
perl -laF, -ne '$" = q(","); print qq("#F")'
awk -F, -v OFS='","' -v q='"' '{$0=q$0q;$1=$1}7' file
for example, comma sep:
kent $ echo "foo,bar,baz"|awk -F, -v OFS='","' -v q='"' '{$0=q$0q;$1=$1}7'
"foo","bar","baz"
tab sep would be similar.

Using concat() function, is specific cases [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need concatenate regexp pattern pieces, for this pattern, I use C-style Escape E.
If use concatenation operator ||, works:
E'a{'||2||'}'
does not make much sense, but just interes, how to concatenate same, using concat() function ?
The misunderstanding is this: C-style escapes are just another way to input string literals. When you concatenate strings, be it with the || operator or with the concat() function (Postgres 9.1+), the method how individual strings were input is irrelevant.
In addition to that, literals of other types (like the numeric constant 2 in your example) are coerced to text automatically.
On top of that, your example does not exhibit any characters with a special meaning in escape strings (like \).
SELECT E'a{' || 2 || '}';
SELECT concat(E'a{', 2, '}');
So, the E is totally irrelevant in this particular example.
Since you mention regexp patterns: those tend to have \ in them, which have to be escaped with \ in E'' notation:
SELECT E'\\.' || 2 || '\.';
The modern way is not to use escape strings at all if not necessary. That's why Postgres switched to standard_conforming_strings = ON with PostgreSQL 9.1. That is the setting I tested with.