guys.
There is a file named 'server.conf' and I want to use shell to change content from it.
In line 115, there is server-bridge 192.168.50.225(ip) 255.255.0.0(mask) 192.168.10.50(begin ip) 192.168.10.90(end ip)
in it. I want to change the ip, mask, begin ip and end ip. For example, I plan to change
`server-bridge 192.168.50.225 255.255.0.0 192.168.10.50 192.168.10.90`
into
`server-bridge 192.168.10.100 255.255.0.0 192.168.10.60 192.168.10.80`
What should I do with sed or others tools? Thanks a lot.
sed -i 's/server-bridge\ 192.168.50.225\ 255.255.0.0\ \ 192.168.10.50\ 192.168.10.90/server-bridge\ 192.168.10.100\ 255.255.0.0\ \ 192.168.10.60\ 192.168.10.80/' server.conf
You can also create a simple script where new values to be replaced are stored in $ip ..etc.... sed -i will do in place editing to the file.
The best tool I have used is Vi.
(sudo) vi /home/mydoc.txt will open and allow you to do any editing you need done. If you have never used Vi before, there are some great HOW-TO's and tutorials online. Here is one:
http://www.howtogeek.com/102468/a-beginners-guide-to-editing-text-files-with-vi/
But I'd encourage you to really read and experiment on test files before you change the file you are referencing, AND, PLEASE, make a backup up the file ( cp /home/mydoc.txt mydoc.txt-orig ) before you do. You can always remove the edited file that does not work, but restoring the original after extensive exiting can be a hair-pulling experience.
you can use sed to do this for example some thing like this
Note:every space is escaped by \ sed Intro
cat server.conf | sed 's/server-bridge\ 192.168.50.225\ 255.255.0.0\ \ 192.168.10.50\ 192.168.10.90/server-bridge\ 192.168.10.100 255.255.0.0\ \ 192.168.10.60\ 192.168.10.80/' > server.conf
or you can use two files for safety like this
cat server.conf | sed 's/server-bridge\ 192.168.50.225\ 255.255.0.0\ \ 192.168.10.50\ 192.168.10.90/server-bridge\ 192.168.10.100 255.255.0.0\ \ 192.168.10.60\ 192.168.10.80/' > server.conf.bak
cat server.conf.bak > server.conf
You can use this awk:
awk -v ip='192.168.10.100' -v mask='255.255.0.0' -v bip='192.168.10.60' \
-v eip='192.168.10.80' '/server-bridge/{$2=ip "(ip)"; $3=mask "(mask)"; $4=bip "(begin";
$6=eip "(end"} 1' server.conf
Using sed to change all lines containing server-bridge:
sed -i -e '/^server-bridge/!b' \
-e 'c server-bridge 192.168.10.100 255.255.0.0 192.168.10.60 192.168.10.80' input
to change th 115th line only:
sed -i -e '115!b' \
-e 'c server-bridge 192.168.10.100 255.255.0.0 192.168.10.60 192.168.10.80' input
Related
I'm trying to pipe the output of a grep command into the 'replace me with' value in a sed command. I've tried xargs and just a pipe, but I can't seem to get it working. All of the examples I've found on stack overflow assume that I know the end result of my grep command. Here is an example of what I'm trying to do.
cat /etc/sysconfig/network | grep HOSTNAME | grep -i s/greppedline/"HOSTNAME=something"/
Effectively, I won't know the full contents of the line that I need to replace, just the fact that HOSTNAME will be in it. Is there a away to do this as a one-liner without creating a variable from the grep commmand?
I think you're trying to do like this,
sed '/HOSTNAME/s/.*/"HOSTNAME=something"/' /etc/sysconfig/network
Add the inline edit -i option to save the changes made.
sed -i.bak '/HOSTNAME/s/.*/"HOSTNAME=something"/' /etc/sysconfig/network
sed '/HOSTNAME/ c\
"HOSTNAME=something"/' /etc/sysconfig/network
or
sed 's/.*HOSTNAME.*/"HOSTNAME=something"/' /etc/sysconfig/network
I copied a directory structure from a windows box to a Linux box and I would like to use sed to replace c:\IBM\WebSphere with /opt/IBM/WebSphere in all files under this directory.
Any thoughts?
I think sed is a little inconvenient for that purpose, if you want to change the actual files you can use perl one-liner
perl -pi -e 's/c:\\IBM\\/\/opt\/IBM\//g' *
Add or adjust the paths according to what you need (add WebSphere if you want the replacement to change only these dirs)
Since sed can take any character as a delimiter, use
sed -e 's_\\_/_g'
to replace the \ to /.
sed -e 's_[Cc]:_/opt_g'
to replace the c: with /opt
You can string those together:
echo "C:\\IBM\\WebSphere" | sed -e 's_\\_/_g' -e 's_[Cc]:_/opt_g'
Output:
/opt/IBM/WebSphere
I don't see an awk solution, just add one:
awk -F'\' -v OFS='/' '$1=/^c:/?"/opt":$1'
test:
kent$ awk -F'\' -v OFS='/' '$1=/^c:/?"/opt":$1' <<<'c:\IBM\WebSphere'
/opt/IBM/WebSphere
echo "C:\Users\San.Tan\My Folder\project1" | sed -e 's/C:\\/mnt\/c\//;s/\\/\//g'
replaces
C:\Users\San.Tan\My Folder\project1
to
mnt/c/Users/San.Tan/My Folder/project1
in case someone needs to replace windows paths to Windows Subsystem for Linux(WSL) paths
I need to remove the character : from a file. Ex: I have numbers in the following format:
b3:07:4d
I want them to be like:
b3074d
I am using the following command:
grep ':' source.txt | sed -e 's/://' > des.txt
I am new to Linux. The file is quite big & I want to make sure I'm using the write command.
You can do without the grep:
sed -e 's/://g' source.txt > des.txt
The -i option edits the file in place.
sed -i 's/://' source.txt
the first part isn't right as it'll completely omit lines which don't contain :
below is untested but should be right. The g at end of the regex is for global, means it should get them all.
sed -e 's/://g' source.txt > out.txt
updated to better syntax from Jon Lin's answer but you still want the /g I would think
This line works:
sed -r -e 's/^([^#a-z]+)localhost/\1hostname.domain hostname localhost/' /etc/hosts
But adding the itty option "i":
sed -ir -e 's/^([^#a-z]+)localhost/\1hostname.domain hostname localhost/' /etc/hosts
Results in:
sed: -e expression #1, char 60: invalid reference \1 on `s' command's RHS
Can someone tell me what's going on??
You've turned off the -r (extended syntax) option, because what you append to -i isn't more options, but an optional backup suffix. From the manpage:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
So just separate them:
sed -i -r -e 's/^([^#a-z]+)localhost/\1hostname.domain hostname localhost/' /etc/hosts
I think you should separate options: write -i -r and not -ir, since -i may interpret r as the suffix to append to the old unedited file, so that -r is not taken
"-ir" means something different from "-i -r" or "-ri", see the man page.
I've a string which looks like:
BOOT_IMAGE=/boot/vmlinuz-2.6.32-31-generic HTTP_BOOT=192.168.1.133 root=UUID=b4 ro quiet splash
In example "/proc/cmdline"
I would like to extract HTTP_BOOT with sed. My current sed command looks like that.
HTTP_BOOT=$(sed -r 's/^.*HTTP_BOOT=(.*?).*/\1/' /proc/cmdline)
The var HTTP_BOOT should contain 192.168.1.133.
Would be really happy if someone could correct my sed.
Thanks
Try this :
HTTP_BOOT=$(sed 's/.*HTTP_BOOT=\([^ ]*\) .*/\1/' < /proc/cmdline)
You are almost there. Try this sed:
sed -r 's/^.*HTTP_BOOT=([^\ ]*).*$/\1/'
HTTP_BOOT=$(egrep -o 'HTTP_BOOT=[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /proc/cmdline | cut -d '=' -f 2)
Also does a nice minor sanity check on the IP