Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. Show all posts

Sunday, 19 November 2017

sed command usage and examples

SED is a streamline editor screen-oriented utility. The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. The stream editor is ideally suited to performing repetitive edits that would take considerable time if done manually. Sed edits line by line; it reads the next line of the file and repeats the process until it is finished with the file.
Syntax
sed options 'command' filename

Let us use below test.txt file for our sed examples.

This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Print mth line from the file.

Syntax
sed -n np filename

Example
sed -n 2p test.txt

Output
SED is a streamline editor.

Print certain range (n1 to n2) of lines from the file.

Syntax
sed -n 'n1,n2p' filename

Example
sed -n '2,4p' test.txt

Output
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.

Print every nth line starting with the specific mth line

Syntax
sed -n 'm~np' filename

testnew.txt file
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.
This line is added to understand current example.

Example
sed -n '2~3p' testnew.txt

Output
SED is a streamline editor.  
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
This line is added to understand current example.

Print only specific lines from file.
We need to use execute option for this purpose as shown below to display 2nd and 5th line.

Syntax
sed -n -e 2p -e 5p test.txt

Output
SED is a streamline editor.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 

Print line from the file containing specific pattern.

Syntax
sed -n '/pattern/p' filename

Example
sed -n '/edit/p' test.txt

Output
SED is a streamline editor.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.

Print line from the file starting with number.
sed -n '/^[0-9]/p' filename

Print line from the file containing consecutive 4 numbers.
sed '/[0-9]\{4\}/p' file.txt

Print line from the file with specific string in uppercase.

Syntax
sed -n 's/lowercasestring/\U&p' filename

Example
sed -n 's/test/\U&p' test.txt

Output
This is a TEST file to learn sed command.
This is a TEST file to learn sed command.
This is a TEST file to learn sed command.

Use L instead of U for lowercase.

Display particular paragraph from the file

Syntax
sed -n '/startofpara/,/endofpara/p' filename

testfile.txt
This is a test file race
You are so fantastic barry.
Run barry Run.

All the great things happens from small begining.

Super dancer go to dance floor.
Have a rocking performance.

Example
sed -n '/This/,/Run/p' testfile

Output
This is a test file race
You are so fantastic barry.
Run barry Run.

Print lines from particular line number to specific string from the file

Syntax
sed -n 'num,/string/p' filename

Example
sed -n '1,/fantastic/p' testfile

Output
This is a test file race
You are so fantastic barry.

Display the file excluding nth line from the file.

Syntax
sed nd filename

Example
sed 2d test.txt

Output
This is a test file to learn sed command.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Display the file excluding last line from the file.

Syntax
sed '$d' filename

Example
sed '$d' test.txt

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with  various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.

Display the file excluding certain range(n1 to n2) of lines.

Syntax
sed 'n1,n2d' filename

Example
sed '2,4d' test.txt

Output
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Replacing a string with another string

Syntax
sed s/stringtoreplace/newstring/ filename

Example
sed s/test/dummy/ test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with  various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

Replacing multiple strings with another string in the entire file at the same time

Syntax
sed 's/stringtoreplace/newstring/;s/stringtoreplace/newstring/' file

Example
sed 's/test/dummy/;s/usage/operations/' test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn operations of sed command with various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

Replacing a string with other string in the file for specific line

Syntax
sed 'n s/existingstring/newstring/' filename

Example
sed '4 s/test/dummy/' test.txt

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with  various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Replacing a string with other string in the file except specific line

Syntax
sed '!n s/existingstring/newstring/' filename

Example
sed '!4 s/test/dummy/' test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn operations of sed command with  various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

We can also use range instead of number if required.
sed 'n1,n2 s/existingstring/newstring/' filename

Replacing a string with another string in the file for the lines if specific string found

Syntax
sed '/specificstring s/stringtoreplace/newstring/' filename

Replacing a string with another string in the file for all the lines except specific string found.

Syntax
sed '/specificstring/!s/stringtoreplace/newstring/' filename

Example
sed '/sed s/usage/operation/' filename

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn operation of sed command with  various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Inserting BLANK lines in file

Syntax
sed G filename

Example
sed G test.txt

Output

This is a test file to learn sed command.

SED is a streamline editor.

We will learn usage of sed command with various examples.

This is a test file to learn sed command.

The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 

Sed edits line by line.

This is a test file to learn sed command.


To insert two blank lines execute following

Syntax
sed 'G:G' filename

In place editing of the file and performing sed operations on nth occurrence of matched string in the line

Syntax
sed -i 's/replacestring/newstring/n' test.txt

Below example will modify the test.txt file.

sed -i 's/test/dummy/gi' test.txt

g - global replace i.e. replace every instance of occurence of string in the line.
i - ignore case

Below example will modify the test.txt file and also keep the original test.txt file with org extension.

sed -i'.org' 's/test/dummy/gi' test.txt

Executing sed commands through script

Syntax
sed -f script.sed filename

cat script.sed
s/test/dummy

Example
sed -f script.sed test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

Alternative for head command

Syntax
sed nq filename

Example
sed 3q test.txt

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.

Print last line of the file also Alternative for tail-1 command.

Syntax
sed -n '$p' test.txt

Output
This is a test file to learn sed command.

Sunday, 9 July 2017

Cut command usage and examples

Cut 
Cut is used for text extraction. Like its name it cuts given number of characters or field from specified file. The cut command assumes that the fields are separated by tab character. If the fields are delimited by some other character, then option –d is used to set delimiter.

Syntax
cut [options] file_name

Options
-b Select only the bytes from each line as specified in LIST.
-c Select only the characters from each line as specified in LIST.
-d Used for the field delimiter.
-f Select only mentioned fields on each line; also print any line that contains no delimiter character.
--complement Select All Fields Except the Specified Fields.
-s
Do not print lines not containing delimiters.
--output-delimiter=STRING Set STRING as the output delimiter string.

LIST specifies a byte/character, a set of bytes/characters, or a range of bytes/characters. Each LIST is made up of an integer, a range of integers, or multiple integer ranges separated by commas.

c Nth byte, character, or field, counted from 1.
N- From the Nth byte, character, or field, to the end of the line.
N-M From the Nth to the Mth byte, character, or field (inclusive).
-M From the first to the Mth byte, character, or field.

Let us consider below skills.txt file for understanding cut examples.

cat skills.txt
Barry likes coding.
Henry like to travel.
Joseph is an dancer.
Richi is an actress.
Peter was journalist, now he is a manager.   
     
Example               

Command to display specific column from a file

cut -c3 skills.txt 
r
n
s
c
t

The above example displays third character from each line of the file skills.txt.

Command to display specific range of column from a file

cut -c1-4 skills.txt
Barr
Henr
Jose
Rich
Pete

The above example displays character from range 1 to 4 from each line of the file.

Command to display columns from a file beginning from specified Start Position

cut -c4- skills.txt
ry likes coding.
ry like to travel.
eph is an dancer.
hi is an actress.
er was journalist, now he is a manager.

The above example displays starting from 4th column till the last column of each line of the file.

Command to display columns from a file until specified End Position

cut -c-7 skills.txt
Barry l
Henry l
Joseph
Richi i
Peter w

The above example displays columns from 1st to the 7th column of each line of the file.

To display specific field from a file

When we want to display a whole field, need to use option -f and -d. The option -f specifies which field you want to extract, and the option -d specifies the field delimiter that is used in the input file.

cut -d' ' -f2 skills.txt
likes
like
is
is
was

The above example displays the second field in each line by treating the space as delimiter. 

To display multiple fields from a file

We can print more than one field by specifying the position of the fields in a comma delimited list.

cut -d' ' -f2,3 skills.txt
likes coding.
like to
is an
is an
was journalist,
The above example displays 2nd and 3rd fields only. 

To display range of fields from a file

cut -d' ' -f1-2 skills.txt
Barry likes
Henry like
Joseph is
Richi is
Peter was

The above example prints the first and second fields.

To display fields from beginning to the end field specified

In order to print the first two fields, you can ignore the start position and specify only the end position.

cut -d' ' -f-3 skills.txt
Barry likes coding.
Henry like to
Joseph is an
Richi is an
Peter was journalist,

The above example prints up to 3rd field. 

Display fields from start field specified till the ending.

To print the fields from second fields to last field, you can remove the last field position.

cut -d' ' -f2- skills.txt
likes coding.
like to travel.
is an dancer.
is an actress.
was journalist, now he is a manager.

The above example displays fields from 2nd to the last field of each line of the file.

Display fields only when line contains the delimiter

cut -d'#' -f2- skills.txt
Barry likes coding.
Henry like to travel.
Joseph is an dancer.
Richi is an actress.
Peter was journalist, now he is a manager.

In the above example, we have specified the delimiter as #, and cut command displays the whole line, even when it doesn’t find any line that has # as the delimiter.

We need to use -s option to display the lines that contains the specified delimiter.
cut -d'#' -s -f2- skills.txt

No output shown. 

Display all fields except the specified fields

In order to hide certain field use option --complement.

cut -d' ' --complement -f2 skills.txt
Barry coding.
Henry to travel.
Joseph an dancer.
Richi an actress.
Peter journalist, now he is a manager.

The above example displayed all the fields except 2nd field from the file.
For Display changing the output delimiter

By default the output delimiter is same as input delimiter that we specify in the cut -d option. To change the output delimiter use the option --output-delimiter as shown below.

cut -d' ' --complement -f2 --output-delimeter='|' skills.txt
Barry|coding.
Henry|to|travel.
Joseph|an|dancer.
Richi|an|actress.
Peter|journalist,|now|he|is|a|manager. 

The above example has the input delimiter as space , but the output delimiter is replaced by | (pipe).
When we set the –output-delimiter value to $’\n’ then each and every field of the cut command output is displayed in a separate line.

TIPS

The entire file is displayed when you don’t specify a number before or after the ‘-‘.

When calling cut command, use the -b, -c, or -f option, but only one of them.

If no FILE is specified, cut reads from the standard input.