Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a flat file which contains the following data records between H: and T:.

H:20050427 HEADER RECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
0000000 00000 000000000 123456 00 654321 DATARECORD
T:20050427 TRAILER RECORD
[blank space]

[blank space] is a new line (carriage return key) created with no records after T:

How do I remove the blank space(s) (if present) from the file and create a new file with data which are present between H: and T:?

share|improve this question

2 Answers 2

You're kind of asking two different questions; I'm not sure which you're actually trying to do

Remove blank lines

Use awk to match any line with a character on it and only output those:

$ awk '/./ {print}' /path/to/your/file

Only output lines between H: and T:

Use awk to match the lines that start with H: and T: and only output the lines between them (this assumes none of the other lines will have those prefixes):

$ awk '/^H:/,/^T:/ {print}' /path/to/your/file
share|improve this answer

If you just want to remove all [blank space] (may contain any whitespace in general) lines:

sed '/^[[:space:]]*$/ d' filename > newfile

or simply (why bother with awk or sed):

grep . filename > newfile

or to print only between H: and T: lines:

sed -n '/^H:.*/, /^T:.*/ p' filename > newfile
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.