sed - print only the chars that match a given set in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - print only the chars that match a given set in a string
# 1  
Old 08-25-2018
sed - print only the chars that match a given set in a string

For a given string that may contain any ASCII chars, i.e. that matches .*,
find and print only the chars that are in a given subset.

The string could also have numbers, uppercase, special chars such as ~!@#$%^&*(){}\", whatever a user could type in
without going esoteric

For simplicity take for example a string like:

Code:
zabacaaszdeaazfagaaahaaaiazakalmnzzopaaqzrastazauzazvwzaxyz

and a subset such as [a|z].

[1] What we want is to print out just all the 'a' and 'z' chars.

I can do this with grep:

Code:
 echo zabacaaszdeaazfagaaahaaaiazakalmnzzopaaqzrastazauzazvwzaxyz|grep -oz '[a|z]'

or, essentially the same thing

Code:
echo zabacaaszdeaazfagaaahaaaiazakalmnzzopaaqzrastazauzazvwzaxyz|grep -ozE 'a|z'

which yields: zaaaazaazaaaaaaaazaazzaazaazazazzaz

(and could perl it just as easy as well although I have not even tried)

Essentially I want the complementary set of what we get when doing:

[2] Find and print only chars that are neither 'a' or 'z'

which in sed is trivial ...

Code:
 echo zabacaaszdeaazfagaaahaaaiazakalmnzzopaaqzrastazauzazvwzaxyz|sed -e 's/[a|z]*//g'

So, how can we do [1] above just with sed commands?

(I gather that this must also be trivial, but I fail to come up with a sed solution and request you wizardry once again ;-)

Thanks in advance

Robert Nader

------ Post updated at 03:57 PM ------

I just thought about the complementary ...

Code:
echo zabacaaszdeaazfagaaahaaaiazakalmnzzopaaqzrastazauzazvwzaxyz|sed -e 's/[^a&^z]*//g'

# I mean ...

echo zabacaaszdeaazfagaaahaaaiazakalmnzzopaaqzrastazauzazvwzaxyz|sed -e 's/[^(az)]*//g'

It was too trivial after all, should probably delete this post!

Sorry for the bother! ;-)

As MadeInGermany (thanks!) pointed out, the correct form is just
negating the pattern with:

Code:
s/[^az]//g


Last edited by naderra; 08-25-2018 at 01:54 PM..
# 2  
Old 08-25-2018
  1. A [ ] character set has its own syntax.
  2. [a|z] matches the three enclosed characters.
  3. [az] matches the two characters.
  4. [a-z] matches the range a thru z.
If the first character is a ^ then it negates the following character and -ranges.
Let sed delete all but a z
Code:
sed 's/[^az]//g'

You can also use tr for it
Code:
tr -dc 'az'

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match string from two files and print line

Hi, I have been trying to find help with my issue and I'm thinking awk may be able to do it. I have two files eg file1.txt STRING1 230 400 0.36 STRING2 400 230 -0.13 STRING3 130 349 1 file2.txt CUFFFLINKS 1 1394 93932 . + STRING1 CUFFFLINKS ... (9 Replies)
Discussion started by: zward
9 Replies

2. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

3. Shell Programming and Scripting

Print string after the word match

Hi, I have the logs : cat logsx.txt 744906,{"reportingGroups":,"version":"2.0"} 678874,{"reportingGroups":,"version":"2.0"} 193571,{"reportingGroups":,"version":"2.0"} 811537,{"reportingGroups":,"version":"2.0"} 772024,{"reportingGroups":,"version":"2.0"}... (5 Replies)
Discussion started by: justbow
5 Replies

4. Shell Programming and Scripting

Print lines that match regex on xth string

Hello, I need an awk command to print only the lines that match regex on xth field from file. For example if I use this command awk -F"|" ' $22 == "20130117090000.*" 'It wont work, I think, because single quotes wont allow the usage of the metacharacter star * . On the other hand I dont know... (2 Replies)
Discussion started by: black_fender
2 Replies

5. Shell Programming and Scripting

How to print everything after a string match

Hi all, I'm trying to do some work on the authorized_keys file to do a check if there's any information after the hash key.. At the end of the hash key's in the file, there can be an = or == Is there a way to check if anything exists after these equals and if so print it out or else print... (2 Replies)
Discussion started by: Jazmania
2 Replies

6. Shell Programming and Scripting

Remove duplicate chars and sort string [SED]

Hi, INPUT: DCBADD OUTPUT: ABCD The SED script should alphabetically sort the chars in the string and remove the duplicate chars. (5 Replies)
Discussion started by: jds93
5 Replies

7. Shell Programming and Scripting

[Solved] print chars of a string

how can i print all the chars of a string one by line? i have thought that use a for cicle and use this command inside: ${VARIABLE:0:last}but how can i make last? because string is random P.S. VARIABLE is the string or can i make a variable for every chars of this string? this was my idea... (10 Replies)
Discussion started by: tafazzi87
10 Replies

8. UNIX for Dummies Questions & Answers

regexp: match string that contains list of chars

Hi, I'm curious about how to do a very simple thing with regular expressions that I'm unable to figure out. If I want to find out if a string contains 'a' AND 'b' AND 'c' it can be very easily done with grep: echo $STRING|grep a|grep b|grep c but, how would you do that in a single... (9 Replies)
Discussion started by: jimcanoa
9 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. UNIX for Dummies Questions & Answers

Extracting the last 3 chars from a string using sed

Hi. Can I extract the last 3 characters from a given string using sed ? Why the following doesn't work (it prints the full string) : echo "abcd" | sed '/\.\.\.$/p' doesn't work ? output: abcd Thanks in advance, 435 Gavea. (7 Replies)
Discussion started by: 435 Gavea
7 Replies
Login or Register to Ask a Question