07. Writing to file(s)

There is one more flag that wasn't covered that is used with substitution command, the w. This is used to write the outputs to a file.

# sed script
/all/w all.txt
/One/w one.txt
/the/w the.txt
...
# Up to ten output files in one script

Within each instance of sed, you can include up to ten output files. Note the single space between the w flag and the output file name.

You can output a list of files depending on the last digit.

Examples

1) Write to first line

$ sed -n '1w output.txt'

2) Write first and last line

$ sed -n -e '1w output.txt' -e '$w output.txt' oneOS.txt

3) Write lines that match a specific pattern

$ sed -n '/pattern/w output.txt' oneOS.txt

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad