The sed command does nothing - scripting

I am trying to change the name of a file using the sed command and it is just not working.
I have tried:
a)root#ubuntu:/test# sed 's;/test/testfile/;/test/testfile1/;'
b)root#ubuntu:/test# sed "s;/test/testfile/;/test/testfile1/;"
and also tried
c)root#ubuntu:/test#sed 's/\test\/testfile\//\/test\/testfile1\//'
d)root#ubuntu:/test#sed "s/\test\/testfile\//\/test\/testfile1\//"
The cursor enters newline and just hangs there. I have to ctrl+c out of it.Is the syntax wrong or am i running it incorrectly?. I am new to this and just testing this command on my linux system.
Appreciate your help.

If you're trying to rename file test to testfile, then you don't want sed at all. You just want
mv test testfile
The sed command is for manipulating a stream of lines of text, not for renaming files.

Related

How to delete the "0"-row for multiple fles in a folder?

Each file's name starts with "input". One example of the files look like:
0.0005
lii_bk_new
traj_new.xyz
0
73001
146300
I want to delete the lines which only includes '0' and the expected output is:
0.0005
lii_bk_new
traj_new.xyz
73001
146300
I have tried with
sed -i 's/^0\n//g' input_*
and
grep -RiIl '^0\n' input_* | xargs sed -i 's/^0\n//g'
but neither works.
Please give some suggestions.
Could you please try changing your attempted code to following, run it on a single Input_file once.
sed 's/^0$//' Input_file
OR as per OP's comment to delete null lines:
sed 's/^0$//;/^$/d' Input_file
I have intentionally not put -i option here first test this in a single file of output looks good then only run with -i option on multiple files.
Also problem in your attempt was, you are putting \n in regex of sed which is default separator of line, we need to put $ in it to tell sed delete those lines which starts and ends with 0.
In case you want to take backup of files(considering that you have enough space available in your file system) you could use -i.bak option of sed too which will take backup of each file before editing(this isn't necessary but for safer side you have this option too).
$ sed '/^0$/d' file
0.0005
lii_bk_new
traj_new.xyz
73001
146300
In your regexp you were confusing \n (the literal LineFeed character which will not be present in the string sed is analyzing since sed reads one \n-separated line at a time) with $ (the end-of-string regexp metacharacter which represents end-of-line when the string being parsed is a line as is done with sed by default).
The other mistake in your script was replacing 0 with null in the matching line instead of just deleting the matching line.
Please give some suggestions.
I would use GNU awk -i inplace for that following way:
awk -i inplace '!/^0$/' input_*
This simply will preserve all lines which do not match ^0$ i.e. (start of line)0(end of line). If you want to know more about -i inplace I suggest reading this tutorial.

bulk renaming files rearranging file names based on delimiter

I have seen questions that are close to this but I have not seen the exact answer I need and can't seem to get my head wrapped around the regex, awk, sed, grep, rename that I would need to make it happen.
I have files in one directory sequentially named from multiple sub directories of a different directory created using find piped to xargs.
Command I used:
find `<dir1>` -name "*.png" | xargs cp -t `<dir2>`
This resulted in the second directory containing duplicate filenames sequentially named as follows:
<name>.png
<name>.png.~1~
<name>.png.~2~
...
<name>.png.~n~
What I would like to do is take all files ending in ~*~ and rename it as follows:
<name>.#.png where the '#" is the number between the "~"s at the end of the file name
Any help would be appreciated.
With Perl's rename (stand alone command):
rename -nv 's/^([^.]+)\.(.+)\.~([0-9]+)~/$1.$3.$2/' *
If everything looks fine remove option -n.
There might be an easier way to this, but here is a small shell script using grep and awk to achieve what you wanted
for i in $(ls|grep ".png."); do
name=$(echo $i|awk -F'png' '{print $1}');
n=$(echo $i|awk -F'~' '{print $2}');
mv $i $name$n.png;
done

sed with doublequotes and $

I have a bash script that I use to setup a simple php script on my server. I am stuck with how to correctly change a variable with sed with the script. Here is what I have tried:
echo "Enter Portal Password:"
read PORTPASS;
sed -i 's/$ppass =".*"/$ppass ="$PORTPASS"/' includes/config.php
The above changes the variable in the config file but it only changes it to $PORTPASS it does not change it to what I input in the script.
I also tried this and it does change the $PORTPASS correctly, but it remove the " " around the variable in the file.
sed -i 's/$ppass ='".*"'/$ppass ='"$PORTPASS"';/' includes/config.php
Here is what I'm trying to change in the conf.php file: $ppass ="password";
Try:
sed -i "s/\$ppass =\".*\"/\$ppass =\"$PORTPASS\"/" includes/config.php
You have to use double quotes (") around the command so that the shell will evaluate $PORTPASS before passing it to sed, so then you have to "escape" all of the double quotes within the command.

How can I remove table prefixes from a SQL dump?

I have a *.sql file which is dumped from PHPMyAdmin, and all of the tables have a prefix of ff_. How can I remove this? I tried using Notepad++, but it doesn't work because the insert data contains the word too.
Try something like "`ff_" to "`". In simple notepad, notepad++ or sed.
Sed here isn't something different.
For this simple replacement you should create your dump to be forced with "`" around table names.
GNU sed is here to help:
sed -i 's/`ff_/`/g' *.sql
On Mac look for gsed instead of sed. Note the backtick in patterns.
If you think that one of your files contains `ff_ in a string other than table name, you can check that with:
grep '`ff_' *.sql
If this is the case, consider the following:
sed -i 's/INSERT INTO `ff_/INSERT INTO `/g' *.sql

using sed to delete matching line from multiple text files

All files in a directory of my project has one line:
var $useDbConfig = 'default_dev';
How can I delete this line from all files and then save the same file, with a single line command using sed?
you may try
sed -i.bak '/var \$useDbConfig = .*default_dev.*;/d' *
or you can use awk
awk '/var/&&/\$useDbConfig/&&/default_dev/{next}{print $0>FilENAME}' *
The -i argument to sed edits in place. With an argument, it saves a backup. So you want something like this:
STR="var $useDbConfig = 'default_dev';"
sed -i.bak "/$STR/d" *
If your sed version supports the -i option you could use this command to interactively delete the line from all files in current directory:
sed -i "/var \$useDbConfig = 'default_dev';/d" ./*
Furthermore: The way to quote the string works in bash. In other shells (like csh) you should adjust the pattern.