awk/sed print from line number to end of file

I have file with 10 lines where content of first line in 1, second line 2, third line is 4 …… and tenth line is 10.

Here I will show some operations related to read read in lines based their line numbers.

Print from line number 5 to end of file:

$ awk 'NR>=5' inputFile
or
$ sed '1,4d' inputFile

Print from line number 4 to 9 or print lines between linen number 3 and 10:

$ awk 'NR>=4 && NR<=9'  inputFile
or
$ sed -n '4,9p' inputFile

Print all even lines numbers:

$ awk '{if(NR%2 == 0) {print $1}}' inputFile

Print all odd lines numbers:

$ awk '{if(NR%2 != 0) {print $1}}' inputFile

awk/sed get text between two patterns/strings

By using awk/sed command we can get text between two patterns/strings.

I have file with names.txt with following content:

uk
us
india
obama
putin
vatican

With following sed/awk commands I will show how to get text from india to putin:

With sed:

Syntax:

$ sed -n "/Pattern1/,/Pattern2/p" inputFile

Example:

$ sed -n "/india/,/putin/p" namex.txt

Output:

india
obama
putin

With awk:

Syntax:

$ awk "/Pattern1/,/Pattern2/ { print }" inputFile

Example:

$ awk "/Pattern1/,/Pattern2/ { print }" names.txt

Output:

india
obama
putin

Above awk example will work with gawk.

To install gawk use following command:

$ sudo apt-get install gawk

awk/sed print from pattern to end of file

 

By using awk/sed command we can print text from a given pattern to end of file.

I have file with names.txt with following content:

uk
us
india
obama
putin
vatican

With following sed/awk commands I will show how to print text from line “india” to end of file:

With sed:

Syntax:

$ sed -n '/Pattern/,$p' inputFile

Example:

$ sed -n '/india/,$p' namex.txt

Output:

india
obama
putin
vatican

With awk:

Syntax:

$ awk '/Pattern/,0' inputFile

Example:

$ awk '/india/,0' names.txt

Output:

india
obama
putin
vatican

Linux remove blank lines from file

 

To remove all blank lines from a file we can use sed command.

There are 2 ways to delete blank lines with sed

  • Removing blank lines within the file.
  • Redirecting std out to new file by removing blank lines from input file.

Example to removing blank lines within the file:

$ sed -i  '/^$/d' INPUT_FILE

Example to redirect std out by removing blank lines from input file:

$ sed '/^$/d' INPUT_FILE > NEW_FILE

or

$ cat INPUT_FILE | sed '/^$/d' > NEW_FILE

-Sany

Count number of lines in a file – Linux

 

There are different ways to count number of lines in a file.

I will show the some of commands to count number files in file.

With wc:

$ wc -l <inutFileName>

Counting number of lines with wc -l is most common and popular way.

With awk:

$ awk 'END{ print NR }' <inutFileName>

With sed:

$ sed -n '$=' <inutFileName>

With grep:

$ grep -c "" <inutFileName>

-Sany

VIM edit only between specific line numbers

To edit content between specific line numbers use following expression in vim editor

:startLineNumber,endLineNumbers/search/replace/g

In above expression add s after end line number.

Example:

Add # character at a starting of the line from line number 3 10 use following expression in vim:

:3,10s/^/#/g

To replace all foo’s with bar’s from line 3 to 10 use following expressin:

:3,10s/foo/bar/g

We can achieve same functionalities with sed expression also with following expressions:

$ sed -i "3,10s/^/#/g" inputFileName
$ sed -i "3,10s/foo/bar/g" inputFileName

-Sany

By Sandeep Posted in sed, vim

Sed delete line with matching text/string

 

To delete all lines containing particular string/text use following command:

$ sed '/inputString/d' inputFile.txt 

Example:

Delete all line containing word hello and redirect the output to outputFile.txt:

$ sed '/hello/d' inputFile.txt > outputFile.txt

To Delete all lines containing word hello within inputFile.txt use -i option with sed:

$ sed -i '/hello/d' inputFile.txt

-Sany

Linux Add text at specific line number in a file

We can add some text at a specific line number in a file using a simple sed expression.

Example:

$ sed "3n some text here" inputFile.txt

Above example will add “some text here” string in inputFile.txt by adding new line.

Lets consider I have a text file t.txt with content as follows:

$ cat t.txt

1

2

3

In t.txt file I would like to add “hello” at line 3, it should done as follows.

$ sed "3i hello" t.txt

Output:

1
2
hello
3

If you use -i option with sed, it will edit file in place without printing it to screen.

Ex: $ sed -i "3i hello" t.txt

This example shouldn’t print any text on your screen, and your input text file will update automatically.

Now run cat command on your input text and check for your modification, it should be updated.

In our case run cat on t.txt

$ cat t.txt

Output:

1
2
hello
3

-Sany

Delete a line from text file with line number using sed

 

Sed is a stream editor for filtering and transforming text.

With sed we can delete a line using its line number without opening the text file.

Lets test with a example:

$ cat input.txt 

1

2

3

4

input.txt contains 4 lines with content as shown above.

Now I would like to delete line number 3 from input.txt

$ sed -i "3d" input.txt

$ cat input.txt

1

2

4

After running sed command as shown above just check the input.txt file where we can see that line 3 will be deleted.

-Sany