Match pattern in line using grep [closed] - linux

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
I need to grep lines from a file with comma separated values, and direct the output to another file. Thus, if the second value starts with 'U' and the last value is 'Success', then this line matches the pattern.
Should match the pattern:
324,U63#DOM1,U63#DOM1,C1755,C1755,Kerberos,Network,LogOn,Success
Should fail to match:
456,C1164$#DOM1,C1164$#DOM1,C625,C625,?,Network,LogOff,Success
123,U63#DOM1,C11847$#?,C2109,C2109,?,?,TGT,Fail
Thank you!

As columnar matching requirements become more complex, an awk solution becomes more attractive:
awk -F, '$2 ~ /^U/ && $(NF) == "Success"'
(The default action of the match is to print the line.)
Here is the solution using grep though:
grep '^[^,]*,U.*,Success$'
And sed:
sed '/^[^,]*,U.*,Success$/ p; d'

grep '^[^,]*,U.*,Success$'
Look for the first comma, a U, the last comma, Success and end of line.

Related

How to find all lines which contain at least one of a set of words as a prefix [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 1 year ago.
Improve this question
I have a text file of words, one per line, called A.
I have another text file B.
How can I find all lines in B what have at least one of the words from A as a prefix?
I was hoping to be able to do this from the command line maybe using grep but any other command line solution would be great too.
For example, if A is
apple
bob
cheese
and B is
aple
bob123
ches
I would like the line bob123 to be returned.
One approach uses bash's process substitution and sed to add a regular expression beginning-of-line ^ anchor to each line of A, and then tells grep to use it as a list of regular expressions to search for:
$ grep -f <(sed 's/^/^/' a.txt) b.txt
bob123

Replace with sed on csv 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 1 year ago.
Improve this question
I have a csv file and I am trying to substitute the last letter for a word...
The input is
1111;AAA;... (more columns);A1a;A
2222;XXX;... (more columns);T3g;B
... (more rows)
...(more rows)
4564;AdA;... (more columns);G1a;A
33321;B1X; ... (more columns);T3g;B
And I want to replace A for "Avocado" and B for "Banana"...
I tried
#sed -e "s/;A$/;C/g" file.csv
But doesn't work, any advice, please?
Is the following what you're trying to achieve?
tink#host:~/tmp$ sed 's/A$/Avocado/;s/B$/Banana/' file.csv
1111;AAA;... (more columns);A1a;Avocado
2222;XXX;... (more columns);T3g;Banana
... (more rows)
...(more rows)
4564;AdA;... (more columns);G1a;Avocado
33321;B1X; ... (more columns);T3g;Banana
If that looks correct, and you want to change in-file, add a -i to sed.
If you want a new file, add a > new_file to the end of the line.
This seems to work:
sed -i 's/A$/Avocado$/g' file.csv
sed -i 's/B$/Banana$/g' file.csv
The -i replaces the text and the Regex doesn't need the ; because it should use only one character, right? Therefore, one can just specify that character and replace it with a whole word.

Remove entry based on the value of first column [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 5 years ago.
Improve this question
If 3549,2152,4701 in first column then remove the entry:
sample data:
18106|1.0.4.0/22
3549|1.0.10.0/24
5413|1.0.0.0/16
2152|1.4.0.0/16
3549|1.0.8.0/22
4701|1.0.0.0/8
Expedted output:
18106|1.0.4.0/22
5413|1.0.0.0/16
How to achieve this?
For your pattern to match only on the first field you have to anchor the expression to the start of the line:
grep -v -E '^(3549|2152|4701)\|'
The ^ marks the beginning of the line (and $ would mark the end of the line)
The -E activates enhanced regular expressions so you don't have to \ escape pipes and parentheses, and the -v inverses the search (returning only lines that do not match).
The ^ matches the start of the line then parentheses with the pipe symbol marks alternatives (3549, 2152 or 4701), and \| stands for the pipe symbol itself which your first field ends with, and needs to be escaped by the backslash so it's not treated as another alternation.
Be careful to use single quotes around it because otherwise the shell itself will interpret some of the special characters.

How to remove specific characters from a string? [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 am brand new to Perl and struggling with it.
I need to learn just the basics and not much.
I googled and got no simple answer to my question.
I have a string which contains numbers, dots, dashes, colon: and two alphabets.
I want to replace one alphabet by a space and the other by nothing.
How do I do this?
Is there no string.ReplaceChar(theChar, replacement)?
You can try as in the example below:
From commandline:
To replace only the first occurrence of the alphabets:
sdlcb#ubuntu:~$ echo "123.-A456:7B9AB0" | perl -pe 's/A//; s/B/ /'
123.-456:7 9AB0
To replace all occurrences of the alphabets:
sdlcb#ubuntu:~$ echo "123.-A456:7B9AB0" | perl -pe 's/A//g; s/B/ /g'
123.-456:7 9 0
Within script:
#!/usr/bin/perl -w
use strict;
my $data = "123.-A456:7B9AB0";
my $final_data = $data;
$final_data =~ s/A// ;
$final_data =~ s/B/ /;
print "data: $data\n";
print "final_data: $final_data\n";
Use g for substituting all occurrences.
Perl lends itself to using regular expressions, so that is how I would approach it;
#!/usr/bin/perl -w
use strict;
my $ALPHABET = 'abcde';
my $source_data = "1.2.3-$ALPHABET:$ALPHABET";
my $dest_data = $source_data
$dest_data =~ s/(-)$ALPHABET(:)/$1 $2/;
print "source_data: $source_data\n";
print "dest_data: $dest_data\n";
--->
source_data: 1.2.3-abcde:abcde
dest_data: 1.2.3- :abcde
Here the reqular expresion operator (=~) is substituting the first occurance of the pattern (-)$ALPHABET(:) with a '- :' string.
This pattern is using capture groups '()' to locate the match within the data.
Depending on your data you will likely need to adjust the patterns in the capture groups.
The backrefernces $1 and $2 within the match are used for demonstration purposes.
We could help with a more specific regex if you include example data in your question.

Word length and substitute using sed - bash [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 have a file that looks like this:
hell hi tell smith david
abc def ab abcd
123456 123ab
I would like to reverse all the words with length 3 and 4, to get output like this:
lleh hi llet smith david
cba fed ac abcd
123456 123ab
How can I do that using strictly only sed?
Here you go:
sed -re 's/\<(.)(.)(.)\>/\3\2\1/g'
Explanation:
The -r flag is to be able to use extended regular expressions. It's specific to GNU sed. Without this, I would have to write the pattern as: \<\(.\)\(.\)\(.\)\>
\< matches beginning of a word, and \> the end. Both have zero length.
Things matched within (...) can be used in the replacement as \1 for the first expression, \2 for the second (...), and so on.
The g flag at the end is to replace all occurances on the same line, not only the first
In short, the search pattern matches words of length 3, and replaces them with their letters reversed.
I see you updated your example, and you want to reverse words with length 4 too. To do that you can add another expression following the same logic, like this:
sed -re 's/\<(.)(.)(.)\>/\3\2\1/g' -e 's/\<(.)(.)(.)(.)\>/\4\3\2\1/g'

Resources