how to edit a file to keep lines of same length only by using shell command

239 Views Asked by At

I have a file with contents like below.

7f22cebc9330
600e98
7fff1814ff50
7f22cebc95c0
7f22cebc95b8
4002a8
7f22cebc95bc

You can see that some have 12 characters (eg:7f22cebc9330 ), and some have six (eg: 600e98).

How can I edit this file such that only lines with 12 characters are kept in the file, removing all the lines that are NOT of 12 characters length ?

So that my new file would look like this:

7f22cebc9330
7fff1814ff50
7f22cebc95c0
7f22cebc95b8
7f22cebc95bc

I mean by using shell command in linux.

Thanks.

1

There are 1 best solutions below

6
On BEST ANSWER
awk 'length() == 12' input.file > output.file

There are several tools that will allow you to edit the file directly (gnu sed, perl, etc), but doing so is a mistake. Write the output to a new file, and use the shell to rename if necessary.