sysrq
sysrq

Reputation: 135

Delete line with specific number of characters

I have a specific file (file.txt) with several lines.

How is it possible to delete all lines that do not have 12 characters, using sed?

Upvotes: 1

Views: 51

Answers (2)

William Pursell
William Pursell

Reputation: 212424

Not sure why you would use sed. This is much cleaner in awk:

awk 'length == 12' file.txt

Upvotes: 2

oguz ismail
oguz ismail

Reputation: 50795

Use an interval expression to specify the exact number of characters you want to match between the beginning (^) and end ($) of the input record.

sed '/^.\{12\}$/!d' file

Upvotes: 3

Related Questions