Bash: cutting a delimited fragment of each string

Viewed 232

I have a file containing lines that look like this:

GTTCAGAGTTCTACAGTCCGACGATCGGATGAGNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCTCCGAGTNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCCTTATATNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCGAAGTGCNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCAAGTTTTNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCCGACGAANNNNNN

I want to remove the first 26 and final 6 characters from each line. I haven't been able to write a good regular expression to accomplish that using vi, but I'm not sure what else to do. Any suggestions?

Thanks!

3 Answers

Try with grep.

This will keep the last 13 characters and then the first 7, returning only the matching characters (-o) with the Perl-compliant -P flag:

grep -oP ".{13}$" foo.txt | grep -oP ".{7}"

If your file name is foo you can use cut to grab out the range of chars you want:

$ cut -c27-33 foo

This produces:

GGATGAG
TCCGAGT
CTTATAT
GAAGTGC
AAGTTTT
CGACGAA

cut can take a character range, if the lines are a fixed size (they appear to each be 39 characters)

cut -c27-33 file.txt
Related