Reputation: 1161
I am having issues removing the entire line in the sample.txt
cat sample.txt
XYZ -2.4 DNW
ZYY -2.4 138
ZZZ -3.4 200
ZZZ -2.4 DNW
sed '/DNW/d' sample.txt >> output.txt
cat output.txt
XYZ -2.4 #removes the DNW, but not the entire line
ZYY -2.4 138
ZZZ -3.4 200
ZZZ -2.4
What I need is this:
cat output.txt
ZYY -2.4 138 #Need the entire lines removed that matched the 3rd column string DNW
ZZZ -3.4 200
I am new to bash and was wondering whether there is an option to remove the entire line in the text file matching the search criteria?
Thank you!
p.s. I would be interested in possible solutions primarily using bash. However, I am starting to play with python as well and if there are solutions I'd be glad to learn those as well.
UPDATE
It turns out that my original sample.txt file was not formatted somehow correctly. The following fixed the issue, as it changed the rows into the comma delimited format (e.g. x, y, c = treated as a line).
cp sample.txt sample.csv
sed '/DNW/d' sample.csv > output.txt #Please note any of the below user suggested answers/solutions work!
Cheers and thanks for all the help!
Upvotes: 6
Views: 11720
Reputation: 11
Using the -i switch for sed will update the file based on pattern
sed -rie 's/DNW.+$//g' file
Upvotes: 1
Reputation: 1016
Your sed command works for me on OSX. Try to be super explicit and try this:
sed '/^.*DNW.*$/d' sample.txt >> output.txt
Upvotes: 2
Reputation: 366223
This is even easier with grep
than sed
:
grep -v DNW sample.txt >> output.txt
If you want to do it in Python, it's a lot more verbose, but not actually much harder:
with open('sample.txt') as fin, open('output.txt', 'a') as fout:
for line in fin:
if 'DNW' not in line:
fout.write(fin)
Or, if you want it a little briefer (but probably harder for a novice to understand):
with open('sample.txt') as fin, open('output.txt', 'a') as fout:
fout.writelines(line for line in fin if 'DNW' not in line)
Upvotes: 6
Reputation: 7581
You got it nearly right:
sed '/DNW/d' sample.txt >> output.txt
Upvotes: 9