How to search a pattern and remove the line using sed which contains special characters like "ranasnfs2:/SA_kits/prod"
I tried using a variable to hold the complete string and then recall the variable in sed command but it is not working.
echo $a
ranasnfs2:/SA_kits/prod
sed -i '/"$a"/d' test.txt
cat test.txt | grep -i SA
/SA_kits -rw,suid,soft,retry=4 ranasnfs2:/SA_kits/prod
You need to escape the slash character.
Use this for deleting lines which contain a /:
sed '/\//d' file
Related
I have data in below format.
sometext NAME=abc TIME_TAKEN
sometext NAME=xyz{123} REQUEST
I am trying to grep the data between Name and space . I want output in below format
abc
xyz{123}
I tried using sed along with cat like below but it is not working .
cat test.txt | sed 's/NAME=\(.*\)"\+[[:space:]]\+"/\1/g'
awk 'sub(/.*NAME= +/,"") && sub(/ +.*/,"")' test.txt
Regards
Match everything before and after the assignment:
sed -ne 's/.* NAME=\(.*\) .*/\1/p' test.txt
Beware! It will extract the last NAME=... from each line.
This might work for you (GNU sed):
sed -En 's/.*\<NAME=(\S*).*/\1/p' file
Turn off implicit printing -n and turn on extended regexp -E.
Match a word NAME, followed by = and then any number of non-white spaced characters and return the any number of non-white spaced characters.
N.B. The replacement must replace the entire line, hence the .* at the start and end of the regexp.
Try the following command
cat test.txt | sed 's#.*NAME=\(.* \).*#\1#'
Explanation:
cat test.txt | sed 's# .*NAME= \(.* \) .* #\1#'
cat fileName | sed 's# All upto NAME= extract(upto any space) skip all
I have a list of pairs of URLs - I want to find all occurrences of the first element of the pair and replace them with the second. I'm trying to use sed for this but sed escapes characters in my URL. Is there a way to make sed find these URLs (without changing my pairs)?
Here's my code:
while read -r NAME
do
ARG1=`echo "$NAME" | awk '{print $1}'`
ARG2=`echo "$NAME" | awk '{print $2}'`
echo "$ARG1"
echo "$ARG2"
sed -i "s#$ARG1#$ARG2#g" file
done < pagetable
pagetable has the pairs of URLS, and I'm doing the find and replace in 'file'. Since my URLs have special characters, sed isn't interpreting them verbatim.
Replace the metacharacters in the search pattern (\ * ^ $ . /) and in the replacement string (& /) before invoking sed. This assumes that the script is run by Bash.
ARG1="${ARG1//\\/\\\\}"
ARG1="${ARG1//\*/\\\*}"
ARG1="${ARG1//\//\\/}"
for mc in \^ \$ \.; do ARG1="${ARG1//$mc/\\$mc}"; done
ARG2="${ARG2//\\/\\\\}"
ARG2="${ARG2//\//\\/}"
ARG2="${ARG2//&/\\&}"
sed -i "s/$ARG1/$ARG2/g" file
I need to replace text in a file with a Windows-style directory path containing backslash (REVERSE SOLIDUS) characters. I am already using an alternative expression delimiter. The backslashes appear to be treated as escape characters.
How can I keep the backslashes in the output?
$ echo DIR=foobar | sed -e "s#DIR=.*#$(cygpath -w $(pwd))#"
C:gwin64homelit
The desired output is:
C:\cygwin64\home\lit
You'll have to escape metacharacters in sed replacement pattern. Fortunately, there are only three of those: &, \, and a delimiter / (see this question and this). In your case, since you're using # for delimiter, you'll have to escape # instead of /.
You can create a helper shell function (like here):
escapeSubst() { sed 's/[&#\]/\\&/g'; }
and then pass your string through it before giving it to sed, like this:
$ echo DIR=foobar | sed -e "s#DIR=.*#$(cygpath -w $(pwd) | escapeSubst)#"
C:\cygwin64\home\lit
my file contain lines like that:
level0.1.level1.1.level1
level0.1.level1.2.level2
I want to replace ".x." with "{i}"
so the desired lines are like follow
level0.{i}.level1.{i}.level1
level0.{i}.level1.{i}.level2
When we look for ".x.", meaning "." followed by any character
followed by ".", the search pattern for sed will be
"\..\.", as we need to escape the "." with "\".
Once the match is found replace it with ".{i}."
The "/g" at the end implies multiple such replacements.
cat file | sed 's/\..\./.{i}./g'
using sed
sed -r 's/[.][0-9]+[.]/.{i}./g'
crash test
echo 'level0.1.level1.2.level2'|sed -r 's/[.][0-9]+[.]/.{i}./g'
output
level0.{i}.level1.{i}.level2
I know how to remove all trailing spaces from a file, e.g :
sed -i 's/ *$//' file
Is there a way to do it, but not in lines containing only spaces?
Something in the spirit of :
sed -i 's/[a-zA-Z0-9;}{] *$/[a-zA-Z0-9;}{]/' file
^ keep the original characters
Preferably, but not necessariliy, with sed.
Any linux supported solution will do.
Thanks
Just make sure some other character appears before:
sed -r 's/([^\s])\s+$/\1/' file
This checks if a non-space character (\s) appears followed by any amount of spaces. If so, just print this non-space character back, so that the trailing spaces are removed.
Test
Using cat -vet to see the markers:
$ cat -vet a
hello $
$
bye $
$ sed -r 's/([^\s])\s+$/\1/' a | cat -vet -
hello$
$
bye$