Reputation: 179
I was wondering how I could grep all the characters that don't fit a set of characters you want to give it.
For example, say I have a file that has the following text
XXXXX0000000HelloXXXXXX
MyXXXName0000Is00000000
XXXXXJamesXXX0X0X0X0XXX
I want it to print out "HelloMyNameIsJames" or those characters in some other fashion (if it comes out as one character per line, I would be fine with that).
Any help would be greatly appreciated! Raymosrunerx
Upvotes: 1
Views: 103
Reputation: 54452
If you'd prefer one word per line, here's one way using GNU grep
:
grep -oP '(?<=[0X]|\b)[^0X]+(?=[0X]|\b)' file.txt
Results:
Hello
My
Name
Is
James
You can use sed
to neaten up your output:
< file.txt grep -oP '(?<=[0X]|\b)[^0X]+(?=[0X]|\b)' | sed ':loop; N; $!b loop; s/\n/ /g'
Results:
Hello My Name Is James
Upvotes: 1
Reputation: 124
If the lines are contained in file testdata, the following command will print a line each as shown:
sed -e 's/[0X]//g' testdata | xargs
Hello MyNameIs James
Hope this satisfies your needs!
Upvotes: 1
Reputation: 241938
Another solution, one character per line, using grep
(if your grep
supports the -o
):
grep -o '[^X0]'
Upvotes: 1
Reputation: 143906
try:
sed -e 's/[X0]//g' input
grep
isn't going to remove stuff for you.
Upvotes: 0
Reputation: 393239
tr -d X0 < input
Output:
Hello
MyNameIs
James
To remove line ends too:
tr -d 'X0\n' < input
Ouput
HelloMyNameIsJames
(no trailing line end)
Upvotes: 1