Monday, August 20, 2007

Double Space a Single Spaced File -- UNIX

You can use the powerful stream editor (sed) utility to convert a single-spaced file to a double-spaced file. Conversely, you can take a double-spaced file and convert it to a single-spaced file. Here's an illustration.

Single-spaced to double-spaced file
# more tstfile
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test

Now output to stdout and tstfile1 -- double-spaced
# sed G tstfile | tee tstfile1
This is a test

This is a test

This is a test

This is a test

This is a test

This is a test

Verify tstfile1 is double-spaced
# more tstfile1
This is a test

This is a test

This is a test

This is a test

This is a test

This is a test

Double-spaced to single-spaced file
Now output to stdout and tstfile2 -- single-spaced
# sed 'n;d' tstfile1 | tee tstfile2
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test

Verify tstfile2 is single-spaced
# more tstfile2
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test

Extra...
To triple-spaced
# sed 'G;G' tstfile | tee tstfile1

To double-spaced
# sed 'n;n;d' tstfile1 | tee tstfile2

To single-spaced
# sed 'n;n;d' tstfile1 | sed 'n;d' | tee tstfile3

No comments: