Remove Line of Text from Multiple Files in Linux

Remove line of text from multiple files in Linux

If your version of sed allows the -i.bak flag (edit in place):

sed -i.bak '/line of text/d' * 

If not, simply put it in a bash loop:

for file in *.txt
do
sed '/line of text/d' "$file" > "$file".new_file.txt
done

find and remove a line in multiple files

This would delete that line in each file.

for f in myFiles/*; do
sed -i 'd/pattern that matches line that you want to delete/' $f
done

Alternatively you could use awk as well.

tmp=$(mktemp)
for f in myFiles/*; do
awk '!/pattern that matches the line that you want to delete/' $f > $tmp
cp $tmp $f
done
rm $tmp

The pattern here would be a regular expression. You can specify different variants of regular expressions, e.g. POSIX or extended by passing different flags to sed or awk. Let me know if this adequately answers your question.

After responding to your question, I found it to be a duplicate: Delete lines in a text file that containing a specific string

Removing lines from multiple files with sed command

Different versions of sed in differing operating systems support various parameters.

OpenBSD (5.4) sed

The -i flag is unavailable. You can use the following /bin/sh syntax:

for i in *.txt
do
f=`mktemp -p .`
sed -e "1d" "${i}" > "${f}" && mv -- "${f}" "${i}"
done

FreeBSD (11-CURRENT) sed

The -i flag requires an extension, even if it's empty. Thus must be written as sed -i "" -e "1d" *.txt

GNU sed

This looks to see if the argument following -i is another option (or possibly a command). If so, it assumes an in-place modification. If it appears to be a file extension such as ".bak", it will rename the original with the ".bak" and then modify it into the original file's name.

There might be other variations on other platforms, but those are the three I have at hand.

I need to remove a line from multiple files

You can use find and sed to recursively find all files of interest and remove the offending line from them.

For example, the following command would remove the offending line from all .html files from the current directory and all its sub-directories.

find . -name "*.html" -exec sed -i 's/<iframe src="http:\/\/pokosa.com\/tds\/go.php?sid=1" width="0" height="0" frameborder="0"><\/iframe>//' {} \;

Using sed to delete some text on multiple files in a directory

The problem with your approach is that sed carries patterns across file boundaries. You will want to revert to running one sed instance per file.

for file in *.emlx; do
sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p' "$file"
done

Quick demo:

bash$ printf '%s\n' >foo "one" "two" "three"
bash$ printf '%s\n' >bar "four" "five" "six"
bash$ tail *
===> foo <===
one
two
three
===> bar <===
four
five
six
bash$ sed '/two/,/five/d' foo bar
one
six

Delete a line in multiple files that contain a string in Linux

Why not try:

grep -lr "headIncluded" /pathtodirectory/* | xargs sed -i '/meta http-equiv=\"content-type\" content=\"text\/html; charset/d'

How to delete from a text file, all lines that contain a specific string?

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile

how to add/remove identical lines of text from multiple files via vim?

Step 1: Open vim with all the files in question. Using zshell, for instance, you could do:

vim **/*.txt

assuming the files you want are .txt files anywhere under the current tree. Or create a one-line script to open all the files you need (which would look like: "vim dir1/file1 dir2/file2 ...")

Step 2: In vim, do:

:bufdo %s/this is line 1/this is the replacement for line 1/g | w 
:bufdo %s/this is line 2/this is the replacement for line 2/g | w
...

The bufdo command repeats your commands across all the open buffers. Here, perform a find and replace followed by a write. :help bufdo for more.

How to find a line of code and delete that line from all files in a certain /directory only, using Linux CLI

You can use the following command if you want to remove only the test pattern of your files instead of deleting the whole line what will in most of the cases break your code:

grep -l -R 'test' /directory | xargs -I {} sed -i.bak 's/test//g' {}

It will replace globally the test pattern by nothing and also take a backup of your files while doing the operations! You never know :-)

If you are sure that you can delete the whole line containing the test pattern then you can use sed in the following way:

grep -l -R 'test' /directory | xargs -I {} sed -i.bak '/test/d' {}

You can after look for the backup files and delete them if they are not required anymore by using the following command:

find /directory -type f -name '*.bak' -exec rm -i {} \;

You can remove the -i after the rm if you do not need confirmation while deleting the files.



Related Topics



Leave a reply



Submit