Month: June 2017

“SED” Options and its usage

Sed – The Stream Editor

A stream editor is used to perform basic text transformations on an input file. Sed command is mainly used to replace the text in a file. But it is a powerful text processing tool.

Some uses of sed command are explained below with examples:

Consider the text file “test” as an example

1) Replace all the occurrence of the pattern in a line

“SED”  Options and its usage 1

Here in the above example, sed is used to replace all occurrences of  “system” with “software” within the file test.

Let us have look into the switches used for the task

 Denotes
 s  Substitution operation
 /  Delimiter
 system  Search pattern
 software  Replacement string
 g  Global Replacement flag

Sed command by default replace only the first occurrence of a string in each line. The /g flag specifies the sed command to replace all the occurrences of the pattern in each line.

The table below shows some more options:

 Command  Purpose
 sed ‘s/system/software/’ test  Replace first occurrence of a string in each line
 sed ‘s/system/software/n’ test  Replace the nth occurrence of a pattern in a line
 sed ‘s/system/software/ng’ test  Replace from nth occurrence to all occurrences in a line
 sed ‘n s/system/software/’ test  Replace string on a specific line number (nth line)
 sed ‘n,$ s/system/software/’ test  Replace string on a range of lines (from nth line till last)
 sed ‘/as/ s/system/software/’ test  Replace on a line which matches a pattern (here pattern is “as”)
sed ‘s/system/software/w test2’ test Copy replaced line to another file
 sed ‘/your/ c Replaced line’ test  Replace the lines which contains a pattern (In this case, the entire line with the pattern “your” will be replced with “Replaced line”.)
 sed ‘/your/ a Replaced line’ test  Add a new line after a match
  sed ‘/your/ i Replaced line’ test   Add a new line before a match

 

2) Use a different delimiter


sed 's|system|software|' test

Instead of ‘|’ we can also use ‘_’ or ‘@’ as the delimiters. This is mainly used when the search pattern or the replacement string is url.

 

3) Use & as the replacement string


sed 's/system/& and &' test

Here in the above example ‘&’ represents the replacement string and hence replace ‘system’ with ‘system and system’.

4) Duplicate the replaced line


sed 's/system/software/p' test

The /p(print) flag prints the replaced line twice in the terminal. In this case the lines which are not replaced will be printed only once.

The following command generate the duplicate of each line in this file:


sed 'p' test

 

5) Print only the replaced lines


sed -n 's/system/software/p' test

Here the -n option suppresses the duplicate of the replaced lines generated by the /p flag.

If we use the option -n alone without /p, then the sed does not print anything.

 

6) Delete lines


sed '2 d' test

In this case, it deletes the second line from the file ‘test’.


sed '2,$ d' test

Here, it deletes a range of lines i.e, from second line till the last line.

7) Run multiple sed commands

sed -e 's/An/The' -e 's/is/was/' test

In order to run multiple sed commands either we can pipe the output of one sed command to the other or we can use the ‘-e’ option.

8) Sed as grep command


Case 1:
cat test
An operating system is a vital component of the system.
The operating system controls your computer's tasks and system resources to optimize performance.
An operating system is a collection of softwares.
An operating system is abbreviated as OS.

Case 2:
sed -n '/vital/ p' test
An operating system is a vital component of the system.


Case 3:
sed -n '/vital/ !p' test
The operating system controls your computer's tasks and system resources to optimize performance.
An operating system is a collection of softwares.
An operating system is abbreviated as OS.

Here in the above example,

Case 1 shows the contents of the file “test”

Case 2 executes sed command to print the lines which match the pattern “vital”. This is same as grep command.

Case 3 executes sed command to print the lined which do not match with the pattern “vital”. This is same as grep -v.

 

9) Sed as tr command


sed 'y/ATp/atP/' test

The sed command along with y flag acts as tr command. Here in the above example, the sed command use ‘y’ flag to replace ‘A’ with ‘a’ and ‘T’ with ‘t’ which is similar to the action performed by tr command.

 

10) Edit the source file


sed -i 's/system/software/' test

Sed command by default does not edit the source file for our safety but by using ‘-i’ option source file can be edited.

11) Take backup of source file prior to editing


sed -i.back 's/system/software/' test

In the above sed command, before editing the source file sed command takes the backup of ‘test’ as ‘test.back’.

sed command is case-sensitive. Use /I or /i flag for case-insensitive search.

 

There are many more examples of sed command. Here I chose these examples to illustrate some basic concepts. This concludes my tutorial on sed and I hope you enjoyed it.

Get 24/7 expert server management

The post “SED” Options and its usage appeared first on SupportSages – Your IT Infrastructure partner !!.

Some useful switches for “du – disk usage”

As we know, du command is used to check the disk usage of files and folders under Linux system. There are a lot of switches available for du among which here I am trying to explain the most common switches used.

As you know, one can easily checks the switches available for du by looking at the man page for du or by executing the command du –help .

Here I am trying to provide some basic guidelines about commonly used switches so that you can use them whenever in need.

Most commonly used form

# du -sch

h -> human readable format.
c -> display a total size usage at the end of result.
s -> display total size of a file or total size of all files in a directory.

Usage:

root@server [/home/abl]# du -s /home/abl
213932 /home/abl
root@server [/home/abl]# du -sh /home/abl
209M /home/abl
root@server [/home/abl]# du -sch /home/abl
209M /home/abl
209M total

Listing all files and directories, switch “a”

# du -ah

a -> this switch displays disk usage of all individual files and directories.

Usage:

root@server [/home/abl/etc]# du -ah /home/abl/etc | tail
0 /home/abl/etc/wis.com/quota
4.0K /home/abl/etc/wis.com
0 /home/abl/etc/ftpquota
0 /home/abl/etc/quota
4.0K /home/abl/etc/cacheid
0 /home/abl/etc/ablo.com/passwd
0 /home/abl/etc/ablo.com/shadow
0 /home/abl/etc/ablo.com/quota
4.0K /home/abl/etc/ablo.com
28K /home/abl/etc

Exclude something from the command output, using –exclude

# du –exclude

-–exclude -> This switch will avoid the particular file name that we have mentioned.
In the below example du -ah avoid files ending with .txt (–exclude=”*.txt”)

 root@server [/home/abl/etc]# du -ah –exclude=”quota” /home/abl/etc | tail
4.0K /home/abl/etc/wis.abl.com
0 /home/abl/etc/wis.com/passwd
0 /home/abl/etc/wis.com/shadow
4.0K /home/abl/etc/wis.com
0 /home/abl/etc/ftpquota
4.0K /home/abl/etc/cacheid
0 /home/abl/etc/abl.com/passwd
0 /home/abl/etc/ablogsite.com/shadow
4.0K /home/abl/etc/ablogsite.com
28K /home/abl/etc

Display modification time of files/folders, using –time

# du –time

–time ->This option shows the modification date and time of the file/directories.

root@server [/home/user/folder]# du -sch –time *
1.8M    2017-05-19 00:32    Folder 1
248M    2017-05-06 07:35   Folder 2
40K       2016-12-15 15:05   Folder 3
32K       2017-04-27 15:46   File.pdf
250M    2017-05-19 00:32    total

This switches should help you get the disk usage of the files in any directory.

There are few other options for du which are worth trying. You can view its details checking the man page of du.

Get 24/7 expert server management

The post Some useful switches for “du – disk usage” appeared first on SupportSages – Your IT Infrastructure partner !!.

Categories