• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Posts tagged delimiter

Perl> Useful one-liners

Dec08
2010
Written by Scott Rowley

Remove string from file

perl -ni -e 'print unless /pattern/' /path/to/filename

Replace string with string

perl -p -i -e 's/PATTERN/NEW_ENTRY/g' /path/to/filename

Insert string where delimeter is found
This will replace the original delimiter with the new entry and then add the delimiter so you can use it again in the future.

perl -p -i -e 's/DELIMETER/NEW_ENTRY\n\nDELIMETER/g' /path/to/filename

This will grab anything thats in the standard IPv4 format (anything! not always IPs…but usually).

perl -ne 'print "$&\n" while m#\d+\.\d+\.\d+\.\d+#g' file.txt

Search for and find whole paragraphs containing $string

perl -00ne "print if /$string/i" < file.txt
Posted in Perl - Tagged insert, liner, one, one-liner, Perl, remove, replace, string, variable

BASH> Get all but the last field

Oct26
2010
Written by Scott Rowley

Recently I had need of cutting up domain names such as domain.com, this is easy enough for as simple a domain as this. However, the problem arises when you need to cut up something more like “education.k12.ia.us”. In order to get the extension alone in either case you can do the following:

echo domain.com | awk -F. {'print $2'}
com

Now normally if you have a domain as simple as example.com with only one period then you can can just do the opposite of this and print $1:

echo domain.com | awk -F. {'print $1'}
domain

The problem with more complex URLs is that awk’s field delimiter is now looking at multiple periods to look at, could be just one or it could be two, three, four or more.

To get the final extension in this case then we’ll use the following:

echo education.k12.ia.us | awk -F. {'print $NF'}
us

Then for the rest of it we’ll have to reverse it, then cut off the first field then reverse it again:

echo education.k12.ia.us | rev | cut -d. -f2- | rev
education.k12.ia
Posted in BASH - Tagged awk, BASH, domain, extension, field

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top