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

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

Related

Removing empty lines with grep as well as [, ], ', and ,? [duplicate]

This question already has answers here:
Combining two sed commands
(2 answers)
Closed 6 months ago.
sed "s/[][,']//g"
I used this, but then I have empty lines getting returned. I know that you can use:
sed '/^[[:space:]]*$/d'
Delete empty lines using sed
However, trying sed "s/[][[:space:]][,']//g" didn't work as well as other stuff I have tried.
sed is a scripting language; combine the two commands to get the effect you seek.
sed "s/[][,']//g;"'/^[[:space:]]*$/d'

Find and replace SSH command? [duplicate]

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

Using sed to replace value in ini config file [duplicate]

This question already has answers here:
How to escape the ampersand character while using sed
(3 answers)
Closed 2 years ago.
My config file looks like:
KEY1=VALUE1
URL=https://drive.google.com/uc?export=download&id=myhash
KEY3=VALUE3
I'm trying to use sed to replace the URL value with another one. I got to the following:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download&id=mynewhash#g' file.txt
But that doesn't seem to work, as I'm getting:
URL=https://drive.google.com/uc?export=downloadURL=https://drive.google.com/uc?export=download&id=mynewhash=myhash
What am I missing? Thanks
& is a special character in the replacement string provided to the s command of sed. It represents the string that matches the entire regex used to search (URL=.* in your example).
In order to represent itself it needs to be escaped with \:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download\&id=mynewhash#g' file.txt
Type man sed in your terminal to read its documentation or read the documentation of sed online.

Sed find replace combination with less sign and slash and more sign [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 7 years ago.
I have a stumbling block with sed replace (Linux shell)
I need to replace
</test>
to
</test1>
tried
sed -i 's/<\/test>/</test1>/g'
and similar variants -but still no luck...so thanks for any hint to try
Try this:
echo '</test>' | sed 's|</test>|</test1>|'
For what you tried, you need to escape the slash in the replacement string:
sed -i 's/<\/test>/<\/test1>/g'
Or change the regex boundary marker character:
sed -i 's%</test>%</test1>%g'

Using command line to find and replace? [duplicate]

This question already has answers here:
Remove everything after the first / including the first / for each line
(3 answers)
Closed 8 years ago.
I'm trying to use command line to find and replace some text. I have a file with a few million lines that are similar to this:
Something-Here/Grafton-WV</loc>
More-Information/Claremore-OK</loc>
This-Is-It/Seminole-OK</loc>
Your-Company/Lunenburg-MA</loc>
What I need to do is remove the slash and everything after it. I've done wildcard find/replace before but I'm not sure what command would need to be used to start at the slash and continue until the end of the line.
Here's what the output should be:
Something-Here
More-Information
This-Is-It
Your-Company
The following one-liner could work for you:
perl -pe 's{/.*}{}' file.txt
Explanation:
Switches:
-p: Creates a while(<>){...; print} loop for each “line” in your input file.
-e: Tells perl to execute the code on command line.
Code:
s{/.*}{}: Remove all characters after the first forward slash from the line
This is usually done with sed:
sed 's|/.*||' file.txt > newfile.txt