sed command to remove in special character with string [duplicate] - unix

This question already has answers here:
Combining two sed commands
(2 answers)
Closed last month.
I have string as WEB-INF/lib/abc.jar
This has to be removed via usage of sed command and output has to be abc.jar
Input : WEB-INF/lib/abc.jar
Output: abc.jar
I am trying the command separately as
sed 's/WEB-INF//g' | sed 's/lib//g'
However this has to be done in one shot not to be used in different commands .
Please suggest

Using sed
$ sed 's~[^.]*/~~' input_file
abc.jar

Related

Replace groups of characters by newline [duplicate]

This question already has answers here:
Insert newline (\n) using sed
(4 answers)
Closed 2 years ago.
I have a line with ', ' in my file, and I want to replace this with an new line
Input:
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)', 'siteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754', '
[...]
My sed command:
sed "s/\', \'/\n/g"
Output:
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)nsiteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754n
in my output the line break has been replaced by the character n
Why ?
You can use sed like this to use \n in replacement:
sed "s/', '/"$'\\\n'"/g" file
Here we are using $'\n' to use a newline character in replacement. We ended up using ``$'\\n'due to use of double quotes aroundsed` command.
As per man bash:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard
or else with multiline sed:
sed "s/', '/\\
/g" file
This will work on both gnu and POSIX sed versions on bash.
PS: If you're using gnu sed then a simplified command would be:
sed "s/', '/\n/g" file
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)
siteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754
[...]

Unix command for replacing strings which have "/" [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I know that for replacing a string (in a file which has matchstring string), we can use following command
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
How can I use/change the command if my string has special characters like "/"?
For example:
string1: "/home/folder1"
string2: "/home/folder1/folder2"
As #jamieguinan mentioned in his command, almost any delimiter character can be used. So, I changed the command as following: grep -rl matchstring somedir/ | xargs sed -i 's,string1,string2,g' Where string1 and string2 are: /home/folder1 and /home/folder1/folder2, respectively.

unix sed and redirection [duplicate]

This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
Closed 6 years ago.
I am trying to modify a file using sed in a linux server (Ubuntu 16.04).
Here is an example of the code I am running:
sed 's/lineToChange/newString/' example.txt > example.txt
I feel like I should see newString in example.txt after executing this command since the result of the sed command (which prints newString when executed by itself without the redirect) is redirected to overwrite the example.txt content.
Unfortunately the file ends up empty when I do this...
My common sense is telling me that this should be right but clearly there is something I just don't understand here.
If you want to edit a file inline, you should use the -i option:
sed -i 's/lineToChange/newString/' example.txt
This runs sed into a new file and moves that file to example.txt. Whenever you do ">", you essentially empty out example.txt which makes it empty for sed to work on.

Why is SED not accepting my input?

Hi I'm new to unix and trying to make a script that removes punctuation from user input using SED. But it isn't working.
read -p "Please enter a word or sentence: " word
sed -n 's/[^a-zA-Z ]//g' $word
if i enter abcd.,abcd it will give me an error
"sed: can't read abcd,.abcd: No such file or directory"
So I guess that means it is treating the variable $word as a file instead of a string that I want it to process.
How would I fix this?
sed treat argument as input-file.
Try following instead:
echo "$word" | sed -n 's/[^a-z]//ig'
This might work for you (bash):
sed 's/[^a-zA-Z ]//g' <<<"$word"
This utilizes the here-string in bash.

find and replace using sed in unix [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use slashes in sed replace
I need to find the following string /a/b/c and replace it with /r/s/t
It is a string and not a folder
/sam/pam/nancy --> /tim/cook/iphone
I am in the directory and just need to update multiple files having this line.
Use sed to change the files in-place. For example:
sed -i 's|/a/b/c|/r/s/t|g' *.txt
perl -pi -e 's/\/a\/b\/c/\/r\/s\/t/g' file_name

Resources