OneLiners

From FrogspawnWiki

Jump to: navigation, search

These are simple (and not-so-simple) one line commands that perform useful tasks.


Contents

AWK

Tokenise a string

cat sometext.txt | awk 'BEGIN {FS=":"}{print $1, $2, $3, $4}'

For each line of sometext.txt this will tokenise the string with the delimiter of FS (in this case a colon) and prints out the elements 1 2 3 and 4 of the string.

Token variables start from $1, FS can be anything.

If you want to use whitespace as the delimiter then you can forgo the BEGIN statement and just use the following

cat sometext.txt | awk '{print $1, $2, $3, $4}'

The BEGIN stuff gets run before the script, so is used to set the delimiter away from the standard, which is whitespace.

BASH

Strip path from file

echo ${file##*/}


Strip extension from file

echo ${file%.*}

Find

Chmod all directories

find . -type d -exec chmod +x {} \;

Finds and chmods all directories in and below pwd to be globally executable.

Useful if you've just uploaded a load of stuff for a website and your sftp client has set everything to be chmoded 700

Personal tools