Written by Steve Perry
Published on

Using the sed and tr commands to clean up text

I’m currently working on migrating some Contact Form 7 forms to Gravity Forms in WordPress for a site build I’m working on. There are quite a few forms and they have many dropdown form fields in them with quite a few options. Gravity Forms allows you to bulk-add dropdown options but you need a text list with one option per line. The CF7 fields contain a long list double-quote-separated options. I’m pretty lazy when it comes to formatting text so I always look for ways that I can do it quickly using one command if possible. So here’s the way that I found which worked perfectly for me.

So let’s say we want to change this:

“Basic test” “Labourer” “Operative” “Basic Health and Safety Test” “Regular Visitor” “CSCS Manager” “CSCS Supervisor” “Managerial and Professional Person” “Sign Fitters” “Scaffolders” “Shop Fitters” “Bricklayer” “Cleaner” “Carpenter & Joiner” “Ceiling / Partitions Fixer” “Cladding” “Floor Layer” “Ground Worker” “Painting and Decorator” “Plasterer” “Steel Fixer” “Satellite and Aerials” “Telecoms” “Data Installer” “Tiler” “Security Guard” “Demolition” “Plumbing or Gas” “Heating and Plumbing” “Highways” “Ductwork” “Escalator & Lifts” “Pipe Fitting & Welding” “Managerial and Professional Person” “Refrigeration & Air Con” “Service & Facilities” “Working at Heights”

To this:

Basic test
Labourer
Operative

[truncated]

Refrigeration & Air Con
Service & Facilities
Working at Heights

Let’s start by saving our input values to a file called types.txt. We’ll output our results to types-v2.txt. Now we can use a combination* of sed and tr to format our text.

sed 's/" "/|/g' < types.txt | tr '|' '\n' | tr '"' '\n' > types-v2.txt

What this does is searches for ” ” and replaces it with a | symbol using our types.txt file as the input. This then pipes the result to tr which then finds and replaces all | with the \n newline giving us exactly what we need in our output file types-v2.txt.

So there we go. I love stuff like this because even though it took me personally a little while to get the command to work it will now save me loads of time as I work through all of the forms – write once, use many.

*I couldn’t get sed to replace with the newline character for some reason and I couldn’t get tr to find ” ” hence me using a combination of both commands which worked like a dream – feel free to tell me what I’m doing wrong.

Steve Perry Creative Ltd

Studio and registered office: 4 Back Lane, Brown Edge, Staffordshire ST6 8QS.

Copyright © 2012 – 2023 Steve Perry Creative Ltd., unless otherwise noted.

Registered in England & Wales, number 08354632.

Colophon

Typeset in Söhne Kräftig and Söhne Buch, by Klim Type Co.

Set as 32/64, 24/32, 20/32, and 12/16 on an 8px/96px grid.

Colour palette selected for AAA contrast.