Checkout these great crochet books, movie and TV DVDs (some are so new they aren't published yet! pre-order them at amazon.com!), and other items I enjoy or are on my wish list!

Use sed to change string1 to string2 in same file

| 5 Comments


I wanted to change "/artscrafts/" to "/blog/" in all popup image files (these are not stored in the MT database so search-and-replace inside the Movable Type 4.01 administrative interface doesn't work for those files). The file for each popup image is stored in the same directory as the popup image.

The following worked for me, but there is no guarantee that it will work for you. I am not responsible for any problems you may encounter. Backup all files first.

To change a string of characters to a different string of characters in multiple files:

  1. At a Unix prompt, cd to directory where the files to be modified are located
  2. Type on one line: for f in *php; do sed 's/string1/string2/g' $f > $f.new && mv $f.new $f; done

Line 2 means: For every file ending with php, replace string1 with string2 and save the result into a new file, then move the new file over the original file.

For each slash in the string, I needed to use \ in front of it, as follows:

for f in *php; do sed 's/\/artscrafts\//\/blog\//g' $f > $f.new && mv $f.new $f; done

Multiple lines can also be used at a Unix prompt as follows:

for f in *php
do
sed 's/string1/string2/g' $f > $f.new &&
mv $f.new $f
done

Based on information in a thread at dBforums. Note: That code uses the Unix find command to locate files which could be in subdirectories. Back up all files and learn about the find command before using.

for fname in $(find . -name "*" -print)
do
sed '/word/s/word/new_word/g' ${fname} > ${fname}.new &&
mv ${fname}.new ${fname}
done

5 Comments

Very useful!

Thank you very much,.

Herman.

I am trying to compile a script to edit the /etc/login.defs

The file read:


PASS_MAX_DAYS 999999
PASS_MIN_DAYS 0
PASS_MAX_LEN 7


I would like to be able to change it to read


PASS_MAX_DAYS 60
PASS_MIN_DAYS 1
PASS_MAX_LEN 14


I tried the sed command:
sed 's/PASS_MAX_DAYS */PASS_MAX_DAYS 60/g' /etc/login.defs


the result is this


PASS_MAX_DAYS 60 99999999


How do have it replace the entire line with my input?


I have to admit I am a novice and there may be a simple answer so bear
with me


thanks

Hi, Ed - I don't use sed often and am very rusty. You might find these links helpful:
http://www.grymoire.com/Unix/Sed.html
http://student.northpark.edu/pemente/sed/sed1line.txt

Hi Ed,

you have to 'ESCAPE' the special character - an empty space in this case.

You should do it like this:

sed 's/PASS_MAX_DAYS\ 999999/PASS_MAX_DAYS\ 60/g' /etc/login.defs

If you want to replace everything after the 'PASS_MAX_DAYS' string:

sed 's/PASS_MAX_DAYS.*/PASS_MAX_DAYS\ 60/g' /etc/login.defs

Hope this helps.

When working with paths you can use:
sed 's|/usr/bin|/home/usr/|g'

Leave a comment

Sketch of Cheryl

About Cheryl

Enjoys crocheting, gardening, cats, NASCAR (especially Tony Stewart and Kyle Busch!), reading, photography, snorkeling in Kailua-Kona with sea turtles, Sizzler's Mega Bacon Cheeseburgers, hot and iced decaf coffee, dark chocolate, color (yarn, fabric), playing around with web technologies - not necessarily in that order! Still very much a beginner with quilting, knitting, and sewing. Donates crocheted lap blankets.

List maker, detail-oriented, organized, leans heavily toward perfectionism. ISTJ. Libra.

Late 40s. Married to Bill. Son Greg and daughter-in-law Lauren live in Oregon. Stepson Billy and daughter-in-law Rowena are in Maui, Hawaii. Stepson Bryan lives in the Bay Area.

Web Designer for a college in the Bay Area, California. Strives for web accessibility, web standards, valid XHTML, valid CSS.

Tea Party Patriots.
Fiscal responsibility, limited government, free markets.
www.teapartypatriots.org

January 2012

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

I Care About

(Most links open new window.)

  • My wonderful cat Cleo.
  • Give Life. Donate Blood. Red Cross.
  • Learn about breast cancer at BreastCancer.org
  • Tony Stewart 14
  • Stewart-Haas Racing
  • Kyle Busch 18
  • NASCAR
  • YOU on a Diet - RealAge.
  • Get the Firefox web browser - it's Safer, Faster, Better!

TMM Blog ClustrMap

Locations of visitors to this page

Disclaimer

This is a personal website - my place of self expression. The views expressed are mine alone and do not necessarily reflect the views of my employer or any other organization I am a member of or affiliated with.

OpenID accepted here Learn more about OpenID
Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 5.04

About this Entry

This page contains a single entry by Cheryl published on October 22, 2007 12:19 PM.

Redirect URLs with underscores to URLs with dashes (hyphens) was the previous entry in this blog.

Test reCaptcha for comments is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Archives

Categories

Cat playing with strand of yarn.

Recent Comments

  • Jerry: When working with paths you can use: sed 's|/usr/bin|/home/usr/|g' read more
  • jetpac: Hi Ed, you have to 'ESCAPE' the special character - read more
  • Cheryl: Hi, Ed - I don't use sed often and am read more
  • Ed: I am trying to compile a script to edit the read more
  • Herman Jaramillo: Very useful! Thank you very much,. Herman. read more