LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-12-2021, 06:23 PM   #1
iicapn
Member
 
Registered: Mar 2020
Posts: 35

Rep: Reputation: Disabled
Replace all instances of a string in a file


I'm trying to script a hostname change... for instance in my /etc/hosts file, I have a line that says:

Code:
127.0.1.1	debian10.mydomain.com	debian10
If I run the following on the command line, it shows me what I want:

Code:
sed 's|debian10|mybox|g' /etc/hosts
But it doesn't replace my /etc/hosts file... it just shows me the changes.

adding ` > /etc/hosts` to the end doesn't seem to help. Is this the best way to replace a string in a file?

Many thanks in advance :-)
 
Old 02-12-2021, 06:34 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
When in doubt read the manpage.
 
Old 02-12-2021, 06:38 PM   #3
iicapn
Member
 
Registered: Mar 2020
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
When in doubt read the manpage.
You are right, thank you. `-i` does the trick. DUH!
 
Old 02-12-2021, 11:45 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Also remember that -i can take a parameter to append to the backup file. Without any option, it will just overwrite the file without keeping a backup. For example,

Code:
sed -i.orig 's|debian10|mybox|g' /etc/hosts
 
Old 02-13-2021, 04:15 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Lots of comments bemoan the manpages - I use them as the first reference always. Then "info" in need.
Mind you, some could do with polish for clarity - need I mention "man find" ?.
 
Old 02-13-2021, 07:23 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Always be as precise as possible!
A step to more precision is
Code:
sed -i.orig 's|\<debian10\>|mybox|g' /etc/hosts
Now debian10 is substituted but not debian100
Recent GNU Linux also takes (incorporated from PCRE)
Code:
sed -i.orig 's|\bdebian10\b|mybox|g' /etc/hosts
In case /etc/hosts is a link (true on Solaris but not yet Linux), a copy changes the data but leaves the inode (the linkage, the owner, the permissions) intact
Code:
cp -p /etc/hosts /etc/hosts.bak && sed 's|\<debian10\>|mybox|g' /etc/hosts.bak > /etc/hosts
The sed -i takes care about owner and permissions as well - but breaks the link.

Last edited by MadeInGermany; 02-13-2021 at 07:25 AM.
 
1 members found this post helpful.
Old 02-13-2021, 09:24 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
While perhaps not directly applicable to the question at hand, I prefer using specialized tools wherever possible.
Code:
$ hostctl -h

hostctl is a CLI tool to manage your hosts file with ease.
You can have multiple profiles, enable/disable exactly what
you need each time with a simple interface.

Usage:
  hostctl [command]

Available Commands:
  add         Add content to a profile in your hosts file.
  backup      Creates a backup copy of your hosts file
  disable     Disable a profile from your hosts file.
  enable      Enable a profile on your hosts file.
  help        Help about any command
  list        Shows a detailed list of profiles on your hosts file.
  remove      Remove a profile from your hosts file.
  replace     Replace content to a profile in your hosts file.
  restore     Restore hosts file content from a backup file.
  status      Shows a list of profile names and statuses on your hosts file.
  sync        Sync some system IPs with a profile.
  toggle      Change status of a profile on your hosts file.

Flags:
  -c, --column strings     Column names to show on lists. comma separated
  -h, --help               help for hostctl
      --host-file string   Hosts file path (default "/etc/hosts")
      --no-color           force colorless output
  -o, --out string         Output type (table|raw|markdown|json) (default "table")
  -q, --quiet              Run command without output
      --raw                Output without borders (same as -o raw)
  -v, --version            version for hostctl

Use "hostctl [command] --help" for more information about a command.
Code:
$ hostess -h
An idempotent tool for managing /etc/hosts

Commands

    fmt                  Reformat the hosts file

    add <hostname> <ip>  Add or overwrite a hosts entry
    rm <hostname>        Remote a hosts entry
    on <hostname>        Enable a hosts entry
    off <hostname>       Disable a hosts entry

    ls                   List hosts entries
    has                  Exit 0 if entry present in hosts file, 1 if not

    dump                 Export hosts entries as JSON
    apply                Import hosts entries from JSON

    All commands that change the hosts file will implicitly reformat it.

Flags

    -n will preview changes but not rewrite your hosts file

Configuration

    HOSTESS_FMT may be set to unix or windows to force that platform's syntax
    HOSTESS_PATH may be set to point to a file other than the platform default

About

    Copyright 2015-2020 Chris Bednarski <chris@cbednarski.com>; MIT Licensed
    Portions Copyright the Go authors, licensed under BSD-style license
    Bugs and updates via https://github.com/cbednarski/hostess
Found them both via this list.

Last edited by shruggy; 02-13-2021 at 09:29 AM.
 
1 members found this post helpful.
Old 02-13-2021, 05:21 PM   #8
Brains
Senior Member
 
Registered: Apr 2009
Distribution: All OS except Apple
Posts: 1,591

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Same here, here's one I like:
Quote:
hostnamectl set-hostname willy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] replace any line in all files that contains "string <something>" with "string something else" Quakeboy02 Linux - Software 6 04-17-2017 03:30 PM
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
sendmail-2: Wrong number of instances of process sendmail:, expected instances equal maxymaxymaxymaxymaxy Linux - Newbie 1 06-15-2011 10:51 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
need sed help - how to replace all instances of X except those on lines with Y? BrianK Programming 4 03-25-2008 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration