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

Posts tagged awk

AWK> Useful examples

May26
2011
Written by Scott Rowley

Just a starter page that will grow as I find useful awk commands to share.

Convert Linux .txt to Windows .txt
Replace the end of a line with a carriage. Useful for converting .txt files from Linux to Windows.

awk 'sub("$", "\r")' linuxfile.txt > windowsfile.txt

Convert Windows .txt to Linux .txt
And the reverse. Useful for converting .txt files from Windows to Linux.

awk '{ sub("\r$", ""); print }' windowsfile.txt> linuxfile.txt
Posted in BASH - Tagged convert, example, linux, list, sub, txt, Windows

BASH> Remove tabs from output

Oct26
2010
Written by Scott Rowley

In order to remove tabs from your output (say to get a specific variable) use the ‘tr’ command, for example

In my zone.file the line containing “Serial” looks like the following:
If I performed

grep Serial zone.file

I would get the following output

 2009110904 ; Serial (yyyymmdd##)

You can’t see them here but there are several tabs before that date. In my example I want to get just the actual serial number (2009110904). To do this I’ll first strip off the “; Serial…” information;

grep Serial zone.file | awk -F; {'print $1'}

This command is telling it to look for the ; special character and break up the output there, then from there it is told to print the first column of information this results in:

 2009110904

The problem here is that if we want this to be our variable we are actually working with ” 2009110904″ instead of the desired “2009110904” In this particular case we know those are tabs not spaces. So we’ll need to use tr in order to remove those tabs:

grep Serial zone.file | awk -F\; {'print $1'} | tr -d 't'

This will return to us exactly what we want

2009110904
Posted in BASH - Tagged BASH, grep, tab, tr

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 BASH, delimiter, 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