In a file with lots of lines I want to delete lines that starts with HERE IT IS.
How can I do this using only command-line tools?
27 Answers
Try sed:
sed -i '/^HERE IT IS/d' <file>WARNING: Its better to take a backup when using -i switch of sed:
sed -i.bak '/^HERE IT IS/d' <file>The original file will remain as <file>.bak and the modified file will be <file>.
In addition to the very good grep and sed answers you've received, here are some other tools that can do the same thing:
A few Perl ways:
perl -ne '/^HERE IT IS/ || print' file > newfile perl -ne 'print if !/^HERE IT IS/' file > newfile perl -ne 'print unless /^HERE IT IS/' file > newfileYou can add the
-iswitch to any of the examples to edit the file in place:perl -i.bak -ne '/^HERE IT IS/ || print' file(g)awk
awk '!/^HERE IT IS/' file > newfileNewer versions (4.1.1 and later) of GNU
awk(the defaultawkon Linux) can also edit the file in place:gawk -i inplace '!/^HERE IT IS/' fileShell (
bash,zsh,ksh, probably others). This is kind of silly though, it can be done but other tools are better.while IFS= read -r line; do [[ $line =~ ^"HERE IT IS" ]] || printf "%s\n" "$line" done < file > newfile
I would use grep to filter them out. For example :
grep -v "^HERE IT IS" infile > outfileThen move outfile back to infile.
1sed is definitely the way to go.
This slight modification of the command @heemayl gave you will delete the line whether the same case is used in the pattern or not, due to the I in the pattern reference.
sed -i '/HERE IT IS/Id' <file>If you had several files in a directory that you wanted to do this on, you could combine it with find like so.
find . -maxdepth 1 -type f -exec sed -i.bak '/HERE IT IS/Id' {} +The maxdepth option means this won't recurse into directories.
Another python option:
#!/usr/bin/env python3
[print(l, end = "") for l in open(f).readlines() if not l.startswith("HERE IT IS")]Where f is the path to the file, between quotes.
0Grep
grep -P '^(?!HERE IT IS)' file(?!HERE IT IS) negative lookahead assertion which makes the regex engine to match all the line starting boundary (which is usually matched by ^) only if it's not followed by the string HERE IT IS
python
#!/usr/bin/python3
import sys
fil = sys.argv[1]
with open(fil) as f: for line in f: if not line.startswith('HERE IT IS'): print(line, end="")Save the script in a file, say script.py and then run it through the below command on the terminal.
python3 script.py infile 3 You can use Vim in Ex mode:
ex -sc 'g/^HERE IT IS/d' -cx filegglobal searchddeletexsave and close