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.