using sed to delete matching line from multiple text files - file-io

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.

Related

SUBSTITUTING VARIBALE IN SED ESCAPING $

I have 20 lines in file i want to remove all lines after nth line
nth line number is set using varible
eg var1=5 then remove line 5 to 20 using sed
Tried
sed ""$var1",$d" file -i
But it produces following error
sed: -e expression #1, char 5: unexpected `,'```
We can try
var="3"
awk -v line="$var" '1; FNR==line{exit}' file > temp && mv temp file
Details:
Here we are creating a shell variable var with value 3, then we are passing that variable to awk program, now the awk program is printing every line till the line number is 3 then the program is exited.
Use this Perl one-liner:
export var1=5
perl -i.bak -pe 'last if $. == $ENV{var1}' in_file
The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.
$. : Current input line number.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
I was beginner to linux thats why i asked such question.
Since no one answered.
I kept trying.
Now i learned how to debug linux shells and hence while using set -x i encountered mistake i was doing while writing up this command.
Now it is solved.
var=2
sed ""$(echo "$var")",\$d" file

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.

WSL: bash-cmd.exe interoperability: get rid of carriage return [duplicate]

I am new to shell script. I am sourcing a file, which is created in Windows and has carriage returns, using the source command. After I source when I append some characters to it, it always comes to the start of the line.
test.dat (which has carriage return at end):
testVar=value123
testScript.sh (sources above file):
source test.dat
echo $testVar got it
The output I get is
got it23
How can I remove the '\r' from the variable?
yet another solution uses tr:
echo $testVar | tr -d '\r'
cat myscript | tr -d '\r'
the option -d stands for delete.
You can use sed as follows:
MY_NEW_VAR=$(echo $testVar | sed -e 's/\r//g')
echo ${MY_NEW_VAR} got it
By the way, try to do a dos2unix on your data file.
Because the file you source ends lines with carriage returns, the contents of $testVar are likely to look like this:
$ printf '%q\n' "$testVar"
$'value123\r'
(The first line's $ is the shell prompt; the second line's $ is from the %q formatting string, indicating $'' quoting.)
To get rid of the carriage return, you can use shell parameter expansion and ANSI-C quoting (requires Bash):
testVar=${testVar//$'\r'}
Which should result in
$ printf '%q\n' "$testVar"
value123
use this command on your script file after copying it to Linux/Unix
perl -pi -e 's/\r//' scriptfilename
Pipe to sed -e 's/[\r\n]//g' to remove both Carriage Returns (\r) and Line Feeds (\n) from each text line.
for a pure shell solution without calling external program:
NL=$'\n' # define a variable to reference 'newline'
testVar=${testVar%$NL} # removes trailing 'NL' from string

The sed command does nothing

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.

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.