Find the answer to your Linux question:
Results 1 to 3 of 3
is there a way to replace text strings in a file from the command line (no scripts or programming)? I want to replace this line in /etc/inittab Code: id:5:initdefault: with ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! MunkyJuce69's Avatar
    Join Date
    Jun 2007
    Posts
    20

    command for replacing text?


    is there a way to replace text strings in a file from the command line (no scripts or programming)?

    I want to replace this line in /etc/inittab
    Code:
    id:5:initdefault:
    with

    Code:
    id:3:initdefault:
    (replace 5 with 3)


    im making a test and trying to make setup into a single multipart command. what would the command line be?

    thx

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    The sed utility can do pattern matching and replacement. So for your particular example:
    Code:
    sed -e 's/id:5:initdefault/id:3:initdefault/' /etc/inittab
    This would print the modified file to the terminal. You could redirect that into a temp file and replace the original with it. Or you could use the '-i' option to backup the existing copy and automatically make the change. Check the man page for more details.

    sed uses a technique called regular expressions to match patterns in the file. You can do very powerful things with it: find an online resource on regular expressions for more info.

  3. #3
    Just Joined! MunkyJuce69's Avatar
    Join Date
    Jun 2007
    Posts
    20

    Thumbs up

    thank you, worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •