Find and replace SSH command? [duplicate] - sed

This question already has answers here:
How to replace a whole line with sed?
(6 answers)
Closed 1 year ago.
hello I am needing help to be able to replace a line of a file with ssh and sed.
The problem is the following I have a string for example:
mystring = 1234
The point is that after the = it is not always 1234, so I am not able to replace that line.
sed -i 's/mystring =/mystring=1234/g' file.ini
when I run it two things happen:
mystring = 12341234 (add the numbers)
mystring = 123412345 (don't delete it, leave it there)
and if the variable (sed -i 's/mystring=/mystring=1234/g' file.ini) is executed more than twice, what it does is add the number at the end.
That is why I need to replace the entire line, something that if it is done more than 1 you see it will always remain the same, and if it is different, change it to the value that is set in the command.
From already thank you very much.

You can always match and replace the whole line:
sed -i 's/^mystring=.*/mystring=newvalue/' yourfile
.* in a regular expression matching any number of characters, including none.
You can also change all lines containing a certain pattern:
sed -i '/^mystring=/c\
mystring=newvalue' yourfile
Or with GNU sed:
sed -ie '/^mystring=/c\' -e 'mystring=newvalue' yourfile

Related

Keep lines containing "list of different words" like pattern [duplicate]

This question already has answers here:
How to make sed remove lines not matched by a substitution
(4 answers)
Boolean OR in sed regex
(4 answers)
Closed 4 years ago.
How can I keep all lines matching all those words
toto OR titi OR clic OR SOMETHING and delete any other lines?
If I do sed '/toto/ p ' file I cannot select titi for example.
What I am looking for is something similar to a Perl Regular expression as
^ (word1|word2|word3|andsoon).*. However, I need it for sed because it will be integrated into a bigger sed script.
The goal is to keep all lines starting with word where word is any word from a set of words.
The answer here depends a bit on how your master script is called. Imagine you have a file with the following content:
foo
car
bar
and you are interested in the lines matching "foo" and "bar", then you can do:
sed '/foo\|bar/!d'
sed -n '/foo\|bar/!d;p'
sed -n '/foo\|bar/p'
all these will output:
foo
bar
If you would just do:
sed '/foo\|bar/p'
you actually duplicate the lines.
foo
foo
car
bar
bar
As you see, there is a bit of different handling depending on the usage of the -n flag.
-n, --quiet, --silent suppress automatic printing of pattern space
source: man sed
In general, my suggestion is to delete the lines you don't need at the beginning of your sed script.

How to reverse all words in line with sed? [duplicate]

This question already has answers here:
Reverse input order with sed
(6 answers)
Closed 4 years ago.
For example, we have:
This is the song that doesn`t end
What sed command will turn it into this?
end doesn`t that song the is This
I've found only how to reverse lines in a file (a.k.a. tac):
sed -n '1!G;h;$p'
This might work for you (GNU sed):
sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file
Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.
Could you please try following and let me know if this helps you.
awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}' Input_file

Sed to replace last character on condition

I have a file which has following lines
172XI207 X123955 1
412XE401 XE05689 1
412XI402 XI9515 1
412XI403 XI06702 1
412XE404 XE75348 1
I want to replace last column to 2 if the first two characters in the second column matches to XE.
The result should be like below
172XI207 X123955 1
412XE401 XE05689 2
412XI402 XI9515 1
412XI403 XI06702 1
412XE404 XE75348 2
I wanted to use sed (not awk). Can someone please let me know how this can be acheived using sed?
many sed commands take an address or address range (see the man page for the gory details). Probably the most common command is s of course, but it is among those that take an address range, meaning it doesn't need to apply to every line. An address range xan be a regular expression. The s command is:
{address}s/pattern/replacement/
For you the address - matching RE - is / XE/ (assuming your columns are space separarated; change that to a tab if necessary), the pattern is 1$ and the replacement 2. Therefore:
/ XE/s/1$/2/
or as a command line
sed -e '/ XE/s/1$/2/' < oldfile > newfile
EDIT: oops, second column, not start of line.
This command should do the trick (providing you are looking at myfile.txt)
sed -e '/ XE/ s/1$/2//' myfile.txt
You can make sure your replacement is acted by adding the -i option which will modify the file in-place, make sure it's exactly what you are expecting before though.
Edit: based on question in comments, here is a command that matches on 3rd column and replaces on fifth.
sed -e 's/^\(\(\w\+\W\+\)\{2\}XE\(\w\+\W\+\)\{2\}\)1/\12/'
Or, as an alternative, you can first select the line and then substitute:
sed -e '/^\(\w\+\W\+\)\{2\}XE/ s/^\(\(\w\+\W\+\)\{4\}\)1/\12/'

How to pass a variable to sed [duplicate]

This question already has answers here:
Shell variables in sed script [duplicate]
(5 answers)
Closed 8 years ago.
I want to delete words into a line. For example:
I want to delete one word in this line
And I want to delete 'one' to obtain:
I want to delete word in this line
By passing the word through a variable. So far I have got:
WORD=one ; sed -n 's/"$WORD"//g' file.txt > newfile.txt
But, it doesn't do anything. Why not? And how can I make it work?
WORD=one ; sed -e "s/$WORD//g" file.txt > newfile.txt
the key moment is variable expansion. You have to be careful though because shell variable expansion may be sometimes not what you want. In hard cases you have to do something like this:
EXPANDVAR=one; NOEXPANDVAR=another; sed -e 's/'"$EXPANDVAR"'$NOEXPANDVAR//g' file.txt > newfile.txt
In this case sed will replace (remove) pattern one$NOEXPANDVAR , literally.

Matching strings even if they start with white spaces in SED

I'm having issues matching strings even if they start with any number of white spaces. It's been very little time since I started using regular expressions, so I need some help
Here is an example. I have a file (file.txt) that contains two lines
#String1='Test One'
String1='Test Two'
Im trying to change the value for the second line, without affecting line 1 so I used this
sed -i "s|String1=.*$|String1='Test Three'|g"
This changes the values for both lines. How can I make sed change only the value of the second string?
Thank you
With gnu sed, you match spaces using \s, while other sed implementations usually work with the [[:space:]] character class. So, pick one of these:
sed 's/^\s*AWord/AnotherWord/'
sed 's/^[[:space:]]*AWord/AnotherWord/'
Since you're using -i, I assume GNU sed. Either way, you probably shouldn't retype your word, as that introduces the chance of a typo. I'd go with:
sed -i "s/^\(\s*String1=\).*/\1'New Value'/" file
Move the \s* outside of the parens if you don't want to preserve the leading whitespace.
There are a couple of solutions you could use to go about your problem
If you want to ignore lines that begin with a comment character such as '#' you could use something like this:
sed -i "/^\s*#/! s|String1=.*$|String1='Test Three'|g" file.txt
which will only operate on lines that do not match the regular expression /.../! that begins ^ with optional whiltespace\s* followed by an octothorp #
The other option is to include the characters before 'String' as part of the substitution. Doing it this way means you'll need to capture \(...\) the group to include it in the output with \1
sed -i "s|^\(\s*\)String1=.*$|\1String1='Test Four'|g" file.txt
With GNU sed, try:
sed -i "s|^\s*String1=.*$|String1='Test Three'|" file
or
sed -i "/^\s*String1=/s/=.*/='Test Three'/" file
Using awk you could do:
awk '/String1/ && f++ {$2="Test Three"}1' FS=\' OFS=\' file
#String1='Test One'
String1='Test Three'
It will ignore first hits of string1 since f is not true.