Skip to content

How to Remove First Few Characters on All Lines

  • by
  • August 17, 2014March 30, 2018
If you'd like to remove the first few characters with fixed width on all lines, you can try the following methods. For example, we have a file as this:
[root@localhost ~]$ cat temp.txt
123). Database Administration.
124). Web Development.
125). IT Architecture Design.
126). Project Management.

We'd like to remove the numbering part which is at position 1 to 6. Let's see how to use sed to replace them to none.
[root@localhost ~]$ cat temp.txt | sed 's/^......//g'
Database Administration.
Web Development.
IT Architecture Design.
Project Management.

You can see we have 6 dots in the above command to represent the first 6 characters. This could become awkward when there're hundreds of characters need to be removed.

Next, we use cut to dump them.
[root@localhost ~]$ cat temp.txt | cut -c 7-
Database Administration.
Web Development.
IT Architecture Design.
Project Management.

The above command means that we want to keep the text only from the 7th position of characters to the rightmost.

From the opposite direction, you can do the following command to keep the numbering.
[root@localhost ~]$ cat temp.txt | cut -c -6
123).
124).
125).
126).

As you can see, using cut to remove a fixed width string is easier than using sed.

Leave a Reply

Your email address will not be published.