LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2019, 09:27 AM   #16
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757

Quote:
Originally Posted by blason View Post
Thanks and nice option; however I am looking with Grep if possible.
Then why did you ask for a bash solution in a previous post?
Quote:
Originally Posted by blason View Post
Need that in bash ...
 
2 members found this post helpful.
Old 07-18-2019, 09:28 AM   #17
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
just a quick test of that one loop.
Code:
#!/bin/bash

while read -r line;do
        if [[ ! "$line" =~ [][()\'\"~!\`@/?\>\<\\] ]];then
                echo "$line"
        fi
done < $1
testfile
Code:
[][()\'\"~!\`@/?\>\<\\]

[ in here ] 
'what'
< if >
@googles

~where
!ho
Hello
results
Code:
[userx@arcomeo testdir]$ ./stripme testfile


Hello
tells a story...
Just a few modification

Code:
#!/bin/bash

while read -r line;do
        if [[ ! "$line" =~ [][()\'\"~!\`@/?\>\<\/\*\_\+\=\;\:\,\#\$\%\^\&] ]];then
                echo "$line"
        fi
done < $1
 
Old 07-18-2019, 09:28 AM   #18
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by crts View Post
And what story would that be?
looks like someone needs to read the foot notes. it works..
 
Old 07-18-2019, 09:29 AM   #19
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by crts View Post
Then why did you ask for a bash solution in a previous post?
my bad bash as in wanted in grep
 
Old 07-18-2019, 09:36 AM   #20
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by blason View Post
my bad bash as in wanted in grep
Is the sample file you provided at least representative?
 
Old 07-18-2019, 09:50 AM   #21
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MadeInGermany View Post
Better name the printable characters, and use the complement of it, either with tr and -c option, or with a negating ^ in a charset in a RE:
Code:
tr -dc '.a-zA-Z0-9\n-' < samplefile
sed -n 's/[^a-zA-Z0-9\n-]//gp' < samplefile
Those solutions will also keep invalid domain names, like '%rtt.com'. It will "transform" to 'rtt.com' but I am not sure if this is desired by OP.

@OP:
Please provide a sample output file of what you expect it to look like before we keep guessing.
 
1 members found this post helpful.
Old 07-18-2019, 09:57 AM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
If you want to not print lines that have a forbidden character, with grep:
Code:
grep -v '[^a-zA-Z0-9-]' testfile
Again, it is easier to name the allowed characters.
For the [a-zA-Z0-9] set there is [[:alnum:]], can be augmented with extra characters and of course with the ^ negation:
Code:
grep -v '[^[:alnum:]-]' testfile
 
2 members found this post helpful.
Old 07-18-2019, 10:04 AM   #23
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by MadeInGermany View Post
If you want to not print lines that have a forbidden character, with grep:
Code:
grep -v '[^a-zA-Z0-9-]' testfile
Again, it is easier to name the allowed characters.
For the [a-zA-Z0-9] set there is [[:alnum:]], can be augmented with extra characters and of course with the ^ negation:
Code:
grep -v '[^[:alnum:]-]' testfile
Yep, the alnum was given earlier, and promptly ignored.
 
Old 07-18-2019, 10:33 AM   #24
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MadeInGermany View Post
Again, it is easier to name the allowed characters.
For the [a-zA-Z0-9] set there is [[:alnum:]], can be augmented with extra characters and of course with the ^ negation:
Code:
grep -v '[^[:alnum:]-]' testfile
On a side note, let me just point out for OP that if you want to include '-' inside '[]' then it must be the last character. This is the pitfall I was refering to earlier. Otherwise it will be interpreted as a range operator. This solution will almost bring you there. You still need to figure out the last missing part.
 
2 members found this post helpful.
Old 07-18-2019, 12:00 PM   #25
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by crts View Post
On a side note, let me just point out for OP that if you want to include '-' inside '[]' then it must be the last character. This is the pitfall I was refering to earlier. Otherwise it will be interpreted as a range operator. This solution will almost bring you there. You still need to figure out the last missing part.
Yes, so adding more extra characters must be like this
Code:
grep -v '[^[:alnum:].-]' testfile
or this
Code:
grep -v '[^.[:alnum:]-]' testfile
Actually it is possible to have the - character first (after the ^ of course), but there are other characters like a ] that must be first, so it is better to remember "- must be last".
Quote:
Originally Posted by TB0ne View Post
Yep, the alnum was given earlier, and promptly ignored.
Please promptly ignore any sarcasm
 
3 members found this post helpful.
Old 07-18-2019, 01:34 PM   #26
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MadeInGermany View Post
Actually it is possible to have the - character first (after the ^ of course)
Did not know that it can be also first, always had it last when needed and never questioned it. Makes sense, though, since on position one (ignoring ^) it cannot be mistaken to indicate a range.
 
Old 07-18-2019, 09:48 PM   #27
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
Yes, so adding more extra characters must be like this
Code:
grep -v '[^[:alnum:].-]' testfile
or this
Code:
grep -v '[^.[:alnum:]-]' testfile
Actually it is possible to have the - character first (after the ^ of course), but there are other characters like a ] that must be first, so it is better to remember "- must be last".

Please promptly ignore any sarcasm
It wasn't ignored but mistakenly it wasn't working as I was just using [[:alnum:]] hence was showing different results.
 
Old 07-19-2019, 11:23 PM   #28
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Hello,

That worked perfectly fine; however what I am trying to match here is and not sure if this can be achieved in the same line.
Since the above pattern is catching single dot as liternal and hyphen. Being a domain name those will be surrounded by alnum hence trying hard for validation to match . and - only if surrounded by \wfollowed by those two literals.

May be I am missing something?

Quote:
cat test | grep -v [^[:alnum:]\w.-]
 
Old 07-20-2019, 06:17 AM   #29
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by blason View Post
May be I am missing something?
The most important thing you are missing is to provide a representative sample file and the output you expect.
You have been presented with a solution that works for the sample file you provided. Now you are telling us that the sample file is not representing the actual input data, thus the solution is inappropriate. It is pointless to provide you with a solution if you keep changing the requirement.

Last edited by crts; 07-20-2019 at 06:19 AM.
 
Old 07-20-2019, 07:05 AM   #30
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
wrong post.. oops

Last edited by BW-userx; 07-20-2019 at 07:28 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Find with an exclude/exclude file metallica1973 Linux - General 8 11-06-2011 09:39 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Can we use exclude option in"rm" command to exclude some files/folders? yadav_rk727 Linux - Newbie 1 02-03-2010 10:14 AM
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 12:36 AM
tar --exclude --exclude-from cefn Linux - Software 4 10-11-2005 07:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration