Skip to content
Advertisement

How to get lines of a file with exactly 3 characters in Linux

I need a command to find the lines of a file that have 3 characters. I do the command wc -w field.txt to list how many lines, but now I need to know the lines with 3 characters, exactly.

Advertisement

Answer

grep -c -E "^.{3}$" input.txt

This will count (-c) the number of lines which match the regex (-E) “^.{3}$”, which is “start of line, exactly three of anything, end of line”.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement