sed

Use sed to edit files in place (and make a backup copy)

Yesterday I ran into a situation where I had to edit 250,000 files, and of course I instantly thought of the Unix/Linux sed command. I knew what edit commands I wanted to run (simple swap/replace commands), but my bigger problem was how to edit the files in place.

A quick look at the sed man page showed that I needed to use the -i argument of the sed command:

Use sed to modify files in place

sed command FAQ: How can I use the Unix/Linux sed command to edit (modify) files in place?

The short answer is that you just need to use the -i or --in-place sed arguments, as shown in the sed man page:

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied)

Then, if you have a file named 'hello.txt' with contents like this:

jello, world
this is a test

you can then run a sed command like this to modify that file:

How to run a sed command from the Linux command line

Linux sed FAQ: How do I run a Linux sed command from the command line?

I usually only use the Linux sed command in sed scripts, but today I needed to do something much easier than normal, and as I thought about how to run a sed command from the Linux command line, I had to pause for a few moments. Finally I remembered the sed command line syntax, and it looks like this:

sed 's/THE_DATE/2010-07-11/' < sitemap-orig.xml > sitemap.xml

This sed command can be read like this:

Use the Linux sed command to delete a range of lines

In a previous blog post I demonstrated how to use sed to insert text before or after a line in many files, and in this example I'd like to demonstrate how to delete a range of lines using sed.

sed delete - How to delete a range of lines using sed

The problem I had today was that I just re-generated 99 HTML files for my Introduction to Unix/Linux tutorial using Latex2HTML, and it generates a bunch of "junk" in my HTML files that looks like this:

sed example - insert text before and after existing lines

If you ever need to use the Unix/Linux sed command to insert text before or after a line of text in an existing file, here's how I just ran several sed commands to update my old Function Point Analysis tutorial to have a format that doesn't look like it was created in the 1990s.

This tutorial consists of over 40 files, and I had eight changes I wanted to make each file. So I had two choices: modify each file by hand over the next six hours, or run a series of sed commands and be done in 30 minutes. (I chose the sed commands.)

Remove non-printable ASCII characters from a file with this simple Unix command

For a variety of reasons you can end up with text files on your Unix filesystem that have binary characters in them. In fact, I showed you how to do this to yourself in my blog post about the Unix script command. (There's nothing wrong with this approach; it's just a by-product of using the script command.)

sed swap command examples

Summary: This post shares some Unix/Linux sed command swap/replace examples, where you replace one string or regular expression with another string.

Here's the source code for a sed script that I used to convert some poorly formatted HTML into a format that I preferred. I ran this on a set of almost 30 JSP files from an open source project I recently worked on. The good news is that the previous format of the files was consistent, so I was able to use this sed script to convert all the files at once.

A Bourne shell script that loops through all files

Linux shell script for loop FAQ: Can you share an example of a Linux shell script for loop, for instance, to do something for every file in the current directory?

Here's a shell script that you'll find on all the Unix, Linux, and Mac OS X computers I've worked on. The general process of this script is "for every file in the current directory do XYZ".

Linux sed command - use sed and wc to count leading blanks in a file

I use JSPs and servlets to generate a lot of the pages around here, and today I looked at how many blank spaces and blank lines are generated by the JSP's. I don't think I can do much about the blank lines (actually, I just haven't looked into it yet), but about those blanks spaces ...

Out of curiosity I decided to look at this -- how many blank spaces are there at the beginning of lines that I could delete just through formatting? Would deleting those characters help reduce my bandwidth costs (at the expense of slightly uglier JSP's)?

Use the Linux sed command to modify HTML content

A long time ago I created something I called a "Source code warehouse" that would help developers learn various programming languages by letting them easily find examples from open source programming projects from around the world. I initially did this for Java programs, and later expanded it to include source code files from other languages.

Syndicate content