Ksh - finding pattern in file and generating a new file

I am trying to find for the pattern in first 5 bytes of every line in a text file and then generate a new file with the pattern found.

Example: expected pattern is '-' to be serached in first 5 bytes of file only.

Input File

ab-cd jan-09
ddddd jan09
cc-ww jan09
dsgdq jan-09

Output Required :

ab-cd jan-09
cc-ww jan09

Can anyone suggest how to achieve this using unix command or script?

You can use grep to search for lines not having - in its first five positions and use -v option to print all other lines(i.e lines having - in first five chars)

$ cat t
ab-cd jan-09
ddddd jan09
cc-ww jan09
dsgdq jan-09
$ grep -v '^[^-]\{5\}' t
ab-cd jan-09
cc-ww jan09
$

Hey Thanks :slight_smile: This is working.