How do you escape a double quote while using sed? - file

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

Related

Batch Comment in Multi-Line Command

I am wondering if it is possible to have a comment within a batch file command. Specifically, I have a long SED command like the following:
#SED -r -e "s/.../.../"^
-e "s/.../.../"^
-e "s/.../.../"^
fileName >outFileName
I would like to add a comment to each of the "-e" options, as indicated in the following examples:
:: Option #1: At the end of the line
#SED -r -e "s/.../.../"^ // First comment
-e "s/.../.../"^ // Second comment
-e "s/.../.../"^ // Third comment
fileName >outFileName
:: Option #2: Between lines
#SED -r
#REM First comment
-e "s/.../.../"^
#REM Second comment
-e "s/.../.../"^
#REM Third comment
-e "s/.../.../"^
fileName >outFileName
Is there any way to accomplish this?
Give this a try. I don't have sed so I just tested with echo.
#echo off
:: Option #1: At the end of the line
echo SED -r -e "s/.../.../" %= First comment =%^
-e "s/.../.../" %= second comment =%^
-e "s/.../.../" %= third comment =%
:: Option #2: Between lines
echo SED -r^
%= First comment =%^
-e "s/.../.../"^
%= second comment =%^
-e "s/.../.../"^
%= third comment =%^
-e "s/.../.../"
pause
Output
SED -r -e "s/.../.../" -e "s/.../.../" -e "s/.../.../"
SED -r -e "s/.../.../" -e "s/.../.../" -e "s/.../.../"
Press any key to continue . . .

while cycle not stopping in bash

My script is designed to get files (and folders, eventually) and then list them with a while iteration.
format should be:
1) file.txt
2) newfile.txt
3) new folder
and it works! but then the while cycle wouldn't stop. given that i'm a newbie in bash can you tell me what am i doing wrong?
#!/bin/bash
shopt -s nullglob
FILES=(*)
var2="0"
while [ ${FILES[var2] -n } ] ; do
echo "$var2 ${FILES[var2]}"
((var2++))
done
The syntax is [ -n "string to check" ]:
#!/bin/bash
shopt -s nullglob
FILES=(*)
var2="0"
while [ -n "${FILES[var2]}" ] ; do
echo "$var2 ${FILES[var2]}"
((var2++))
done
You can also just do:
#!/bin/bash
shopt -s nullglob
num=0
for file in *
do
echo "$num) $file"
(( num++ ))
done
If this is the basis for a selection menu, you can instead use a select loop which would be much simpler.

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.

Grepping the multiple words from file

I have one file (approx 1000 words).
1.txt:
XYZ
ABC
DEF
GHI
...
And I have another file 2.txt which contains some data, now I want to grep these words in 1.txt in the file 2.txt.
I have used the below logic, but an error is given.
name=$(cat 1.txt |tr '\n' '|')
grep -E -w ${name} 2.txt
Grep can read patterns from a file using the -f option:
egrep -w -f 1.txt 2.txt
you need quotes and use egrep
egrep -w "$(cat 1.txt |tr '\n' '|')" 2.txt
but the answer with -f is better

bash delete a line from a file, permanently

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

Resources