Re: OT: grep and regex patterns

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Once upon a time, Mike Wright <nobody@xxxxxxxxxxxxxxxxxxxx> said:
> PATTERN="^.*http:\/\/.*\.in.*$"
> grep $PATTERN < file.of.links >links.in

Several issues I see:

- it appears you are using a shell variable to pass the pattern; since
  you are using double quotes, shell interpolation occurs, so all the
  escaping \ characters are just escaping from the shell - grep just
  sees "^.*http://.*.in.*$";

- you'll match any URL with "in" in it anywhere, not just in the
  hostname portion

- "^.*" and ".*$" are essentially useless, because they match anything
  at the start and end of the line respectively (which, since by default
  a pattern isn't anchored to the start/end, is not needed)

- you don't need to escape the / (so // is fine instead of the "leaning
  toothpicks" of "\/\/")

- if you are going to use a variable to set the pattern, you need to use
  double quotes around it when it is used

- may not be a problem for your case, but you won't match HTTPS URLs

- minor nit: grep reads from a file, so shell redirection is superfluous

Putting all that together, I'd recommend:

  PATTERN='https?://[^/]*\.in/'
  grep "$PATTERN" file.of.links > links.in

or just:

  grep 'https?://[^/]*\.in/' file.of.links > links.in

Only potential oddity would be if you have URLs with non-standard ports
specified (like "https://foo.in:8080/";); to match that, you could use
egrep instead (extended regex):

  egrep 'https://[^/]*\.in(:[0-9]+)?/' file.of.links > links.in

-- 
Chris Adams <linux@xxxxxxxxxxx>
--
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://lists.fedoraproject.org/admin/lists/users@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux