Find and replace a string in Perl [closed] - perl

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following command line:
perl -i -pe 's/_GSV*//g' file.fasta
My goal is change some sequences that have the following pattern:
GSVIVG01006342001_GSVIVT01006342001
I want to find all sequences that starts with _GSV and finish with anything (that`s why I put the '*') and substitute for nothing.
When I run my command it just recognize the _GSV and return to me that:
GSVIVG01006342001IVT01006342001
and I want that:
GSVIVG01006342001
Can anybody tell me what's wrong with my command line?

before the *, include a dot that means any character
perl -i -pe 's/_GSV.*//g' file.fasta
You can also include the symbol $ to ensure you arrive until the end of the string
perl -i -pe 's/_GSV.*$//g' file.fasta

Related

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 remove a string between a pattern using sed / awk command in linux [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
How can I remove the letter f from the below mentioned string in a file:
a;b;c;d;e;f;g;h;i;j;k;l;m
This needs to be done only by using delimiter ; using sed or awk.
The output will be:
a;b;c;d;e;g;h;i;j;k;l;m
This might work for you (GNU sed):
sed 's/[^;];//6' file
$ echo 'a;b;c;d;e;f;g;h;i;j;k;l;m' | sed 's/;*f;*/;/'
a;b;c;d;e;g;h;i;j;k;l;m
easier using perl pie than sed (unless sed has added an inplace-edit flag in the last 20 years).
perl -p -i -e 's/;f;/;/' fileName.txt
sed 's/f;//' YourFile
be carefull if f is only a sample pattern for the sample due to possible special character in a généric pattern

How to find specific number patterns in a data file [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 have a data file that looks like this
15105021
15105043
15106013
15106024
15106035
15105024
15105042
15106015
15106021
15106034
and I need to grep lines that have sequence numbers like 1510603, 1510504
I tried this awk command
awk /[1510603,1510504]/ soursefile.txt
but it does not work.
Using egrep and word boundary on LHS since OP wants to match all matching numbers on RHS:
egrep '\b(1510603|1510504)' file
15105043
15106035
15105042
15106034
An shorter awk
awk '/1510603|1510504/' file
Based on the contents of your file the following should suffice
grep -E '^1510603|^1510504' file
If your grep version does not support the -E flag, try egrep instead of grep
If you insist on awk
awk '/^1510603/ || /^1510504/' file
Think this works:
egrep '1510603|1510504' source
Your question is very poorly stated, but if you want to print all numbers in the file that begin with either 1510603 or 1510504, then you can write this in Perl
perl -ne 'print if /^1510(?:603|504)/' sourcefile.txt

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.

Change "<path>" value with different string depends on last line [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
Change "<path>" value with different string depends on last line. In that case when see in the last line "*" to replace "<path>" with "ls -lrt" and to separate the "*" from the last line when see slash for anything else with "find".
Text file:
<path>/etc/inet.d/*.conf
<path>/etc/rc/*
<path>/etc/rc*
Expect View:
find /etc/inet.d/*.conf
ls -lrt /etc/rc/ *
ls -lrt /etc/rc*
I think you mean last character of each line, not last line!
if it is right, check this out:
awk '{if($0~/\*$/)sub(/<path>/,"ls -lrt ");else sub(/<path>/,"find ")}7' file
with your data:
kent$ echo "<path>/etc/inet.d/*.conf
<path>/etc/rc/*
<path>/etc/rc*"|awk '{if($0~/\*$/)sub(/<path>/,"ls -lrt ");else sub(/<path>/,"find ")}7'
find /etc/inet.d/*.conf
ls -lrt /etc/rc/*
ls -lrt /etc/rc*