Archive for August, 2006

Prevent non-root users from logging

Friday, August 25th, 2006

Imagine that for some reason (i.e. maintenance tasks) you want to prevent non-root users from logging into the system. The next tip is a very simple way to achieve this goal.

If a file called /etc/nologin exists login will disable the begin of a session in this system. If you put some text into the file, users will be shown this text and their login attempts will be refused.

vi /etc/nologin

Server under maintenance. No access allowed at this moment.

Sed basics

Thursday, August 3rd, 2006

According to sed’s man page…

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

Next, we are going to explore some of the basic usage of sed.

Substitute every occurrence of string1 with string2. The g stands for global, which means that all matching occurrences in the line would be replaced and the output would be directed to outputFile instead of the default standard output.

sed ‘s/string1/string2/g’ inputFile > outputFile

From inputFile show me lines 5732 to 5797. Note the importance of the n switch and the p command to print only the range of lines specified. Without n sed will print the entire file:

sed -n ’5732,5797p’ inputFile

Now I want sed to print everything but lines 5732 to 5797:

sed ’5732,5797d’ inputFile

A kind of cat inputFile1 >> inputFile2:

sed r inputFile1 inputFile2

Show me a paragraph of inputFile that begins with the words “simple mail transport protocol” and ends with the word “postfix”:

sed -n ‘/simple mail transport protocol/,/postfix/p’ inputFile

Next trick will delete empty lines or lines that only contain space characters. Note that the caret denotes the begining of a line, de dollar symbol denotes end of line, the period a single character and asterisk matches zero or more occurrences of the previous character.

sed -e ‘/^ *$/d’ inputFile

Add blank lines to inputFile:

sed ‘G’ inputFile > outputFile

And now delete those blank lines:

sed ‘n;d’ outputFile

A kind of cat inputFile | wc -l:

sed -n ‘$=’ inputFile

Number each line of inputFile:

sed = inputFile | sed ‘N;s/n/t/’

References: