How to remove specific characters from a string? [closed] - string

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.

Related

How to remove set of matching characters at the end of the string in a shell scripts [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 6 months ago.
Improve this question
I want to remove set of matching characters at the end of the string in a shell script. It should work in all the linux flavours, ideally with out using tools like sed,awk.
I found some examples on web but all of them are about removing a single character type.
Below is a set of examples which shows what I am trying to achieve.
Please help.
1. Input : test_-
Output: test
2. Input: test-_-
Output: test
3. Input: test1__-
Output: test1
I want to remove the all the "hyphen" and "underscore" characters from the end of the string.
Since you are tagging this zsh:
Assuming that your string is stored in a variable input, you can do a
if [[ $input =~ ^((.*[^-_])) ]]
then
output=$MATCH
fi
The .* does a greedy match, which guarantees that the last character is neither a dash nor a hyphen.
In bash, this works similar, only that you have to set
output=${BASH_REMATCH[1]}
Supposing your data is in a file, like
test_-
test-_-
test1__-
with grep
grep -oP '[a-z]*[0-9]*' data.txt

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.

Match pattern in line using grep [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
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.

using printf to create a header [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 very new to this, so my apologies in advance if this is a simple question.
I would like to create an output using 'printf' that would look like below:
#-------------------------------------------------------
#TEXT1 #TEXT2
#--------------------------------------------------------
I would really appreciate if someone could give me some hints as to how to do this.
Here's one way using long strings with a specific number of characters.
dashes=$(printf "%0.s-" {1..55})
printf "#$dashes\n#TEXT1%32s#TEXT2\n#$dashes-\n" " "
See Bash-Hackers Wiki for detailed information on printf command in bash.
How it works
dashes=$(printf "%0.s-" {1..55}) - uses brace expansion and command substitution to create a string variable of 55 consecutive - characters.
\n - prints a newline character
%32s - prints 32 " " characters
Update
To print three tabs between #TEXT1 and #TEXT2:
dashes=$(printf "%0.s-" {1..55})
printf "#$dashes\n#TEXT1\t\t\t#TEXT2\n#$dashes-\n" " "
\t represents a tab character.
Just print the strings with newlines at the end.
printf '#-------------------------------------------------------\n'
printf '#TEXT1 #TEXT2\n'
printf '#--------------------------------------------------------\n'
You can also use echo, since there's no formatting in the strings. Then you don't need \n at the end.
You could try this,
$ printf "#-------------------------------------------------------\n#TEXT1 #TEXT2\n#--------------------------------------------------------\n";
#-------------------------------------------------------
#TEXT1 #TEXT2
#--------------------------------------------------------

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