How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux?

I’m Damn sure, everyone was googled many times to get an answer for below questions.

  • How To Empty a File in Linux?
  • How To Delete N Lines From a File in Linux?
  • How To Remove Matching String From a File in Linux?
  • How To Remove Empty Lines From a File in Linux?

Are you thinking, these all are very difficult? i would say yes for newbies but no for experienced guys since it is used “sed” command, and most of the Linux guys was already familiar on that.

Whoever are not familiar on “sed” command, i would advise you to spend valuable time on that which will be very helpful in future.

These all are fall under file manipulation tools such as sed, grep, etc,.

We are going to add at least two examples on each section to demonstrate this.

1) How To Empty a File?

Everyone knows about “rm” command and its purpose but in this article we have covered possible methods that will empty your file rather than deleting it. This will help you when you want to empty a file instead of creating new one.

We assume that we had a filename called 2g.txt in the present working directory. Hence, we are going to use the filename in each example.

Simple Redirection

Just use the below shell redirection to empty a file.

# > 2g.txt
or
# :> 2g.txt

Using echo command

Write a null values on the file.

# echo -n > 2g.txt
  • n do not output the trailing newline.

Using truncate command

truncate command used to shrink or extend the size of a file to the specified size, add s option followed by file size to make a file empty.

# truncate -s 0 2g.txt
or
# truncate 2g.txt --size 0

Using /dev/null device

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection. The /dev/null device is a special file, not a directory, so one cannot move a whole file or directory into it with the Unix mv command.

# cat /dev/null > 2g.txt
or

# cp /dev/null 2g.txt
cp: overwrite `2g.txt'? y
or

# dd if=/dev/null of=2g.txt
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000219195 s, 0.0 kB/s
  • dd : is a command
  • if : is input file
  • of : is ouput file
  • /dev/null : is a special 0 byte file

Using Vim Editor

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” in most of UNIX systems.

# ex -sc ':%d|x' 2g.txt
or
# ex -sc ':1,$d|x' 2g.txt
  • ex : Enter into Ex mode
  • s : Silent; do not display prompts
  • c : Run the given ex command upon startup
  • : : Invoke an ex command
  • % : Choose all the lines
  • d : Delete selected lines
  • x : save and close
  • 2g.txt : input filename

2) How To Delete N Lines From a File?

Alternatively we can delete N lines from a file using vim editor as follow.

For demonstration purpose, we are going to add 1 to 10 numeric numbers into 2g.txt file.

# more 2g.txt
1
2
3
4
5
6
7
8
9
10

You have a requirment that needs to be removed top 4 lines (from 1 to 4) from a file, how to do that? the format should be like below follow.

# ex -sc '1d4|x' 2g.txt
  • ex : Enter into Ex mode
  • s : Silent; do not display prompts
  • c : Run the given ex command upon startup
  • 1 : Move to first line
  • 4 : Select 4 lines
  • d : Delete selected lines
  • x : save and close
  • 2g.txt : input filename

The output is follow.

# more 2g.txt
5
6
7
8
9
10

If you want to remove 6 center lines starting from 4, the format should be like below follow.

# ex -sc '4d6|x' 2g.txt

The output is follow. The above command totally removed 6 lines starting from 4th line to 9th line.

more 2g.txt
1
2
3
10

3) Remove Matching String From a File?

sed command is provides an effective and a versatile way of deleting one or more lines from a designated file. The same has been achieved through vi as well.

To demonstrate this example, we are going to add some names into 2g.txt file.

# more 2g.txt
Fedora
Debian
Ubuntu
Windows
Linux
Windows
RHEL
CentOS
Windows
openSUSE

The above file contains three matching strings called Windows, that we are going to remove in the below example.

Using sed command.

# sed -i '/Windows/d' 2g.txt
  • sed : is a command
  • i : edit files in place
  • Windows : Matching string
  • d : Delete the matching string
  • 2g.txt : input filename

Using vi editor.

# ex +g/Windows/d -cwq 2g.txt

The output is follow.

# more 2g.txt
Fedora
Debian
Ubuntu
Linux
RHEL
CentOS
openSUSE

4) Remove Empty/Blank Lines From a File?

How to remove empty lines from a file? this can be done through sed command.

We are going add some empty lines in the above output manually to demonstrate this example.

# more 2g.txt
Fedora
Debian

Ubuntu


Linux
RHEL
CentOS

openSUSE

As you can see the above output file contains empty lines, hence we are going to use sed command to remove them.

# sed -i '/^$/d' 2g.txt
  • sed : is a command
  • i : edit files in place
  • ^$ : is a regular expression to match blank lines
  • d : Delete the matching string
  • 2g.txt : input filename
  • See the output now.

    # more 2g.txt
    Fedora
    Debian
    Ubuntu
    Linux
    RHEL
    CentOS
    openSUSE
    

    References

    Magesh Maruthamuthu

    Love to play with all Linux distribution

    You may also like...

    Shares
    Close
    Please support the site
    By clicking any of these buttons you help our site to get better