sed or awk, copy aside to create anchortext - sed

How is it possible to transform this:
"dir1/dir2/../file1.ext"><
"dir1/dir2/../file2.ext"><
into this:
"dir1/dir2/../file.ext">file1.ext<
"dir1/dir2/../file.ext">file2.ext<
with sed or awk?

Something like this should work:
sed 's_/\([^/>]\+\)"><_/\1">\1<_g' file

Related

How to replace strings in all files using sed?

I want to replace below line with next line in all the files. So what sed pattern is used for this. I have tried lot but not figured that out..
checkToken($token['token'])
checkToken($token)
This is what I have tried
sed -i -- 's/checkToken\(\$token\['token'\]\)/checkToken\(\$token\)/g' get_officers_v2.php
You just need to get your escape-characters (\) on the right place like:
sed -ie "s/\(checkToken(\$token\)\['token'\])/\1)/" get_officers_v2.php

find sed regex for {}, ignoring the string in it

in a text file (on linux system) I have this string:
O\WIN_INFRASTRUKTUR{Windows Fabrik}\FIM{Forefront Identity Manager(Benutzer)}\EXTRA{}
Now, I want to replace the O\WIN_INFRASTRUKTUR{Windows Fabrik}, but I don't know what is standing in {}. It could be empty or text in it.
I try this, but without success:
sed -e 's/O\\WIN_INFRASTRUKTUR{[a-zA-Z0-9]}/O\\WIFI{}/g'
And that must be the Result:
O\WIFI{}\FIM{Forefront Identity Manager(Benutzer)}\EXTRA{}
Could anyone help me?
use the delimiter as end of your pattern, here it is } so take a class excluding this, any occurrence than your delimiter with [^}]*}
sed -e 's/O\\WIN_INFRASTRUKTUR{[^}]*}/O\\WIFI{}/g' YourFile
sed -e 's/WIN_INFRASTRUKTUR{[^}]*}/WIFI{}/g' <filename>
Thanks, it will be sucessful, but what is, if I want to have this result:
O\WIFI{}\EXTRA{}.
It doesn't matter if I do this:
sed -e 's/O\\WIN_INFRASTRUKTUR{[^}]*}\\FIM{[^}]*}/O\\WIFI{}/g'
than I get only this result: O\WIFI{}

I want to append to a line number using sed

I have a file that I need to append to certain lines.
I can get the line numbers and have been able to use sed to print the entry but not to append the entry.
All I need to do is something like
sed -n '$VAR s/$/,nosuid/' > to_file
Just can not get the syntax down.
Thank you.
Try doing this :
sed "$VAR s/$/,nosuid/" > to_file
Like Etan Reisner said in the comments, the quotes should be double quotes.
This might work for you:
sed -n $VAR's/$/,nosuid/' > to_file

How can i do this using sed command?

Problem : Cannot insert a text using sed
content of file
aa=
i want to add a text after aa= using sed?
the output should be like below
aa=testing
The following should do it:
sed 's/aa=/aa=testing/'
You can try awk if you like.
awk '/aa=/ {$0=$0"testing"}1' file
If you like to make sure it only replace line that only contains aa= and nothing more, do:
awk '/^aa=$/ {$0=$0"testing"}1' file

Substituting IP strings

I am trying to sed strings in file. This is a file, sed_tmp, with one string:
, "127.0.0.2");
This is the sed command I use:
sed -r 's/[0-9{1,3}]\.[0-9{1,3}]\.[0-9{1,3]\.[0-9{1,3}]/XXX.XXX.XXX.XXX/g' ./sed_tmp
and the result is
, "12XXX.XXX.XXX.XXX");
but I need the result
, "XXX.XXX.XXX.XXX");
What am I doing wrong?
Write the {} outside of []
sed -r 's/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/XXX.XXX.XXX.XXX/g' ./sed_tmp
this works:
sed "s/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/XXX.XXX.XXX.XXX/g"
I've never seen the {1,3} syntax that you are using there, do you have a link to somewhere describing it?
Edit: Seems like sed uses it for slightly differently: http://www.grymoire.com/Unix/Sed.html#uh-35