bash delete a line from a file, permanently - file

I have the following code for finding a string in a file then delete the line which contains that string.
echo `sed /$string/d file.txt` > file.txt
the problem is that if initially file.txt contains:
a
b
c
after deleting "a" (string=a) file.txt will become
b c
instead of
b
c
can any one help me?

This is because of the backticks. Do this instead:
sed -i /$string/d file.txt
Note, if you want to do this in-place, you need to use -i to sed as > will destroy the file before sed can read it.

You do not need the echo wrap, simply try:
sed -i '/a/d' file.txt

You need to quote the command's output:
echo -n "sed /$string/d file.txt" > file.txt

sed has an in-place editing option. It's more proper to use that in your senario.
e.g.
sed -i /$string/d file.txt
For the problem of your case, as the output of `` is not enclosed in double quotes, word splitting is done, by bash. And the newlines are removed.
To use echo in this case, do it like this:
echo "`sed /$string/d file.txt`" > file.txt

Related

Replace single quotes around integer values in a text file using batch file

I am new to batch file programming. Need to write a batch file which can remove single quotes around integer values.
Actually, I was writing a batch file to prepare a SQL insert command from text file. Have almost completed but stuck at removing single quotes enclosing an integer.
Suppose, I have a text file, its content is like this.
'abc','def','123','1abc'
'xy','mncef','456','cd'
I would want a batch file to remove that upper quotes around integer and the output should be like this.
'abc','def',123,'1abc'
'xy','mncef',456,'cd'
I have tried below code but it removes all the apostrophe.
#echo off
setlocal enabledelayedexpansion
(for /f "tokens=1-4 delims=," %%a in (sample.txt) do (
set "$line=%%a,%%b,%%c,%%d"
echo !$line:'=!
)
)>out.txt
type out.txt
The Ouput coming is :
abc,def,123,1abc
xy,mncef,456,cd
But I want the single quote to be removed from the position, as integer value will be at a fixed position in each line like here at 3rd.
It's quite easy using common utilities:
$ cat a.txt
'abc','def','123','1abc'
'xy','mncef','456','cd'
$ ./replace.sh
$ cat a.txt
'abc','def',123,'1abc'
'xy','mncef',456,'cd'
And here's the script:
$ cat replace.sh
#!/bin/bash
for i in `egrep -o \'[0-9]+\' a.txt`;do
sed -i "s/$i/${i:1:${#i}-2}/g" a.txt
done;
if you want to use sed
sed -r "s/'([0-9]+)'/\1/g" filename

How to delete a line that contains the dot character using sed

I try to delete from a text file lines that contain the . character.
I try this command, but it does not work:
sed '/./d' input.txt > output.txt
Any ideas?
sed '/\./d' input.txt > output.txt
I think you need to escape the dot.

combine text and files in a script

I have the following problem:
I'm trying to write a script where two files (file1.txt and file2.txt) should be combined into 1 file with a text passage in between. The output should be written in another file (e.g. output.txt).
The output.txt file should be:
[content of file1.txt]
text passage
[content of file2.txt]
After some research on the internet I found the following and it works fine in the terminal:
cat file1.txt <(echo "text passage") file2.txt > output.txt
However, it does not work in my script:
#!/bin/sh
cat file1.txt <(echo "text passage") file2.txt > output.txt
If I execute the script nothing happens (the output.txt isn't written).
Why doesn't this line work in a script and what can I do to make it work?
Thank you for your help!
Stephan
You can just do this:
cat file1.txt > output.txt
echo "Text message" >> output.txt
cat file2.txt >> output.txt
the >> operator means add it to the end of the file, rather than overwriting the contents.
You can also group the commands using brackets:
(cat file1.txt
echo "Text message"
cat file2.txt) > output.txt

Batch: Run an Awk-Script on all text-files in the folder

basically I want to run the following command on every text-file automatically:
awk -f myScript.awk file1.txt > new\file1.txt
awk -f myScript.awk file2.txt > new\file2.txt
...
Then move the processed files to the folder \old.
move *.txt \old
should work for that part.
How do I create the correct for-loop, so that the output of the awk program has the same name as the input, just in the new folder?
OK, try this:
for %%i in (*.txt) do awk -f myScript.awk "%%~fi" > "new\%%~nxi"

How do you escape a double quote while using sed?

I'm trying to remove all lines of text that contain a double quote, and I have tried this:
sed -ne '/\"/!p' theinput > theproduct
It left the lines untouched. What do I do? Here is my script:
`touch tmp.txt
open tmp.txt
read -sn 1 -p "Paste in data and press any key to convert"
echo
touch tmp.txt
open tmp.txt
read -sn 1 -p "Paste in data and press any key to convert"
echo
sed -e 's/-/ /g' tmp.txt > tmp2.txt
grep -v '"' tmp2.txt > final.txt
open final.txt
echo Study Conversion Successful
The first sed command works. It replaces a hyphen with a bunch of spaces (don't ask why I need that). The grep command, which I added from a response, does not work. It leaves the lines with quotes untouched.
Its not necessary to escape the double quote:
sed -ne '/"/!p' theinput > theproduct
Very strange. It "works for me"
$ cat data.txt
dsklfljs
sdjflk"Sdgsd"
sdfj sldkfj "Sdfsd"
sdfj
sdf
sdjflks
$ sed -ne '/\"/!p' data.txt
dsklfljs
sdfj
sdf
sdjflks
Perhaps it is a version issue with sed?
However, you can also consider using grep -v for this.
$ grep -v '"' data.txt
dsklfljs
sdfj
sdf
sdjflks

Resources