Find pattern in file

Problem You need to find a pattern in a file. Using regular expression. Solution Normal command to display something in a file, is grep.grep "pattern"...

SED extract line from file

Problem You want to extract a specific line number from a file. Solution Once you have your line number – see previous post on grep, we can extract...

SED Tips

Problem Following on from the last post on removing lines with sed, how do we use sed to substitute output on the fly? Solution This says substitute occurences...

Read File To Var

Problem You want to read a file into a Perl variable. Solution Following on from the Simple PHP demo of opening and reading contents of files, here is...

List User Login Details

Problem Needing to view current activity of users on the system. Solution Most flavours of UNIX provide a number of tools, to tell you who is logged in...

Setting Up SUDO

Problem You want to allow a user to run one command, as root user. Solution To allow a user access to run stuff as root, just login as root and then add...

Pkill User Procs

Problem Want to kill all processes, for a specific user. Solution This command will send a -KILL signal, equivalent to minus 9:pkill -KILL -U username...

Apache Logging Filter Robots

Problem Sick of filtering through loads of logs, or just spotting real hits from the robots! 🙂Seriously reduce your apache web logs, by filtering out...

Troubleshoot Apache Startup

Problem Here are a few tips, if you are having problems starting apache.1. A common problem is trying to start Apache on a restricted port. This is where...

Job Control

Problem You want to background a process, or leave it running whilst shutting the terminal. Solution To start a UNIX process is the background, simply...

Watch Multiple Files Simultaneously

Problem Performing debugging and you need to view the output, of more than one file at once. Solution A simple way to keep an eye on a file is with tail.tail...

Vi Map Keys

Problem Are there a combinations of key strokes you constantly enter? Solution Maybe you are a Java programming! 🙂 Or PHP/Perl – either way if...

Vi Swap Columns

Problem You need to substitute columns, whilst editing a file with vi. Solution Okay, bit more complex with this one.To substitute columns, we bracket...

Vi Exit

Problem Exiting a file in vi. Solution There are quite a few ways to exit a file in vi. Here are some of my favorites. Example The standard: :wqForce,...

Date Function Calcualate Last Sunday

Problem Showing the date last Sunday, using Perl. Solution Perl has some powerful functions, to perform date manipulation. Such as strftime, localtime...

Converting String Scalar Number

Problem Converting a string scalar into a number. Solution To convert a string scalar to a number, just add zero.Sometimes Perl calculations get a bit...

Convert String Array

Problem Converting a string into an array Solution To convert a string to an array, use split. Example Here we have a string, comprising of fields separated...

Convert String Hash

Problem Need to convert a string to a hash in Perl. Solution Split can also be used to generate a hash, from a string.In this instance split looks at two...

Word Counting

Problem You want to count the words in a file, using Perl. Solution Obviously wc -w can be used for this under UNIX.But fairly decent demo on the power...

Number Output Lines

Problem You want to number each line of output. Solution Again other ways to do this with UNIX, such as grep -vn XXXX filename.But sure there are times...

Perl Doc Usage

Problem You need help on a specific Perl function or module. Or just a general question. Solution Perl installation comes with its own documentation system.In...

Importing AWK SED

Problem You have some way cool awk or sed scripts, but you want to use Perl. Solution Easy. Perl comes with some excellent binaries, which will convert...

Environmental Variables

Problem You want to set something once and have it remembered between UNIX settings. Solution Set some variables. Simplest way to set environmental variables,...

Counting Column Contents

Problem You want to count the contents of a column. Accumulatively totaling each number in a given column. Solution To total up the contents of a given...

Regular Expression Matching NOT

Problem You want to find a line, not matching the pattern. Solution I find this invaluable whilst editing crons. Or in vi – like this: [^x] where...

Finding Disk Space Hogs

Problem Common admin task, to find what is using all the disk space. Solution Simpliest thing to do, in a known directory – for example /var/log...

Display Directory Usage As Percentage

Problem How many times have you been given the job, to clear down space in a UNIX file system?Ever wanted to see the percentage, on a directory by directory...

Duplicate Line Finder

Problem Have you ever duplicated a cron entry, or just wanted to find duplicate lines in a file. Solution Here is a simple one liner, which shows all lines...

Legato Networker Restore

Problem You want to perform a restore from a legato networker backup server.Also called NSR backup system Solution To restore from an NSR (networker) backup...

mysql API socket connection

Problem Sometimes you need to connect to mysql server with a UNIX socket, rather than a Port.Generally this is when running multiple versions, on a box...