I want to change some names in a file using sed.Here i want to replace word which contains only rohit.com with goyal.com.This is how the file looks like:
ServerAdmin webmaster#localhost
ServerName rohit.com
1.rohit.com
rohit.coma
rohit.com.abc
ServerAlias rohit.com
rohit.com
I tried this
sed 's/\brohit.com\b/goyal.com/' a.txt
i received this output
ServerAdmin webmaster#localhost
ServerName goyal.com
1.goyal.com
rohit.coma
goyal.com.abc
ServerAlias goyal.com
goyal.com
But expected output is :
ServerAdmin webmaster#localhost
ServerName goyal.com
1.rohit.com
rohit.coma
rohit.com.abc
ServerAlias goyal.com
goyal.com
please guide me how to fix this.Thanks in advance.
You can use GNU sed solution like
sed -E 's/(\s|^)rohit\.com(\s|$)/\1goyal.com\2/g' a.txt
where (\s|^) matches and captures into Group 1 (\1) a whitespace or start of string, and (\s|$) captures into Group 2 (\2) a whitespace or end of string.
See the online demo:
#!/bin/bash
s='ServerAdmin webmaster#localhost
ServerName rohit.com
1.rohit.com
rohit.coma
rohit.com.abc
ServerAlias rohit.com
rohit.com'
sed -E 's/(\s|^)rohit\.com($|\s)/\1goyal.com\2/g' <<< "$s"
Output:
ServerAdmin webmaster#localhost
ServerName goyal.com
1.rohit.com
rohit.coma
rohit.com.abc
ServerAlias goyal.com
goyal.com
If there can be consecutive matches, repeat the command:
sed -E 's/(\s|^)rohit\.com($|\s)/\1goyal.com\2/g;s/(\s|^)rohit\.com($|\s)/\1goyal.com\2/g' <<< "$s"
#!/usr/bin/env bash
var=goyal
cat > ed1 <<EOF
/ServerName/
s/rohit/$var/
/ServerAlias/
s/rohit/$var/
wq
EOF
ed -s file < ed1
Related
How can I do these in sed?
#input #output
file.txt "nothing"
dir1/ ../
dir1/file.txt ../
dir1/dir2/ ../../
dir1/dir2/file.txt ../../
Let's say #input is placed to $var1
sed "do something" <<< $var1
echo $var1
You can try this GNU sed
sed "s#dir[0-9]\+/*#\.\./#g; s#file\.txt##g"
Is your test for dir1/dir2 (without trailing slash) correct? How does sed know if dir2 is a file or a directory? Otherwise you could use:
echo "dir1/dir2/file.txt" | sed s#[^/]*/#../#g | sed 's#[^/]*$##'
Can anybody help me please?
grep " 287 " file.txt | grep "HI" | sed -i 's/HIS/HID/g'
sed: no input files
Tried also xargs
grep " 287 " file.txt | grep HI | xargs sed -i 's/HIS/HID/g'
sed: invalid option -- '6'
This works fine
grep " 287 " file.txt | grep HI
If you want to keep your pipeline:
f=file.txt
tmp=$(mktemp)
grep " 287 " "$f" | grep "HI" | sed 's/HIS/HID/g' > "$tmp" && mv "$tmp" "$f"
Or, simplify:
sed -i -n '/ 287 / {/HI/ s/HIS/HID/p}' file.txt
That will filter out any line that does not contain " 287 " and "HI" -- is that what you want? I suspect you really want this:
sed -i '/ 287 / {/HI/ s/HIS/HID/}' file.txt
For lines that match / 287 /, execute the commands in braces. In there, for lines that match /HI/, search for the first "HIS" and replace with "HID". sed implicitly prints all lines if -n is not specified.
Other commands that do the same thing:
awk '/ 287 / && /HI/ {sub(/HIS/, "HID")} {print}' file.txt > new.txt
perl -i -pe '/ 287 / and /HI/ and s/HIS/HID/' file.txt
awk does not have an "in-place" option (except gawk -i inplace for recent gawk versions)
Want to change everything after security.server.ip=* with the result ip from the second grep.
First Grep:
cat admin.conf|grep security.server.ip|grep -v ^#
Result:
security.server.ip=10.10.1.2
Second Grep:
cat /etc/hosts|grep -i admin-server|head -1|awk '{ print $1}
Result:
10.10.1.2
Sometimes security.server.ip will be different on admin.conf and I'm wondering how to replace it with one command which will catch IP address form second grep and replace it in the first one.
You can use a script:
#!/bin/sh
IP=$(exec grep -i admin-server /etc/hosts | awk '{ print $1; exit }')
sed -i "/^security\.server\.ip=/s|=.*|=$IP|" admin.conf
You could save it in a variable:
NEWIP=`grep -i admin-server /etc/hosts|head -1|awk '{ print $1}'` \
sed -i "s/^security\.server\.ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/security\.server\.ip=$NEWIP/" admin.conf
With GNU awk for inplace editing, nextfile, and gensub():
gawk -i inplace '
NR==FNR{ if (tolower($0) ~ /admin-server/) { ip=$1; nextfile } next }
{ $0=gensub(/(security\.server\.ip=).*/,"\\1"ip,""); print }
' /etc/hosts admin.conf
I have a example cut down from a log file.
112 172.172.172.1#50912 (ssl.bing.com):
I would like some how to remove the # and numbers after and (): from the url.
Would like the result.
112 172.172.172.1 ssl.bing.com
Here is the sed oneliner I have been working on.
cat newdns.log | sed -e 's/.*query: //' | cut -f 1 -d' ' | sort | uniq -c | sort -k2 > old.log
Thanks
Using sed, you could say:
sed 's/#[0-9]*//;s/(\(.*\)):$/\1/' filename
or, in a single substitution:
sed 's/#[0-9]* *(\(.*\)):$/ \1/' filename
Another sed:
sed -r 's/#[^ ]+|[():]//g'
$ echo '112 172.172.172.1#50912 (ssl.bing.com):' | sed -r 's/#[^ ]+|[():]//g'
112 172.172.172.1 ssl.bing.com
I'm very new to some of the command line utilities and have been looking for a while for a command that would accomplish my goal.
The goal is to find files that contain a string of text, replace it with a new string, and then write the results to a file that is named the same as the original, but in a different directory.
Obviously this is not working, so I am asking how you who know about this stuff would go about it.
grep -rl 'stringToFind' *.* | sed 's|oldString|newString|g' < fileNameFromGrep > ./new/fileNameFromGrep
Thanks for your input!
John
for f in "`find /YOUR/SEARCH/DIR/ROOT -type f -exec fgrep -l 'stirngToFind' \{\} \;`" ; do
sed 's|oldString|newString|g' < "${f} > ./new/"${f}
done
Will do it for you.
If you have spaces in filenames:
OLDIFS=$IFS
IFS=''
find /PATH -print0 -type f | while read -r -d $'' file
do
fgrep -l 'stirngToFind' "$file" && \
sed 's|oldString|newString|g' < "${file} > ./new/"${file}
done
IFS=$OLDIFS
#!/bin/bash
for file in *; do
if grep -qF 'stringToFind' "$file"; then
sed 's/oldString/newString/g' "$file" > "./new/$file"
fi
done
for file in path/to/dir/*
do
grep -q 'pattern' "$file" > /dev/null
if [ $? == 0 ]; then
sed 's/oldString/newString/g' "$file" > /path/to/newdir/"$file"
fi
done
You try:
sed -ie "s/oldString/newString/g" \
$(grep -Rsi 'pattern' path/to/dir/ | cut -d: -f1)
sed:
i in_place
e exec other command or script
grep:
R recursive
s Suppress error messages
i ignore case sensitive