How do I delete lines of a given interval in a file containing a matching word? - linux

I need to use sed to delete from a file only the lines containing a given word. But if my file has 50 lines, I only need to look at the first 30 lines for example.
I know that:
sed '1,30d' f.txt
will delete the first 30 lines of the file.
And:
sed '/Text/d' f.txt
will delete all the lines in the file containing the matching word "Text".
How do I combine these two to solve my problem?
Thank you.

It seems that you want to delete the lines containing Text within the first 30 lines in your file.
You can say:
sed '1,30{/Text/d}' f.txt
If you wanted to delete the first 30 lines and the lines containing Text anywhere in the file, you could say:
sed '1,30d;/Text/d' f.txt

You miss "-i" option:
sed -i '1,30{/Text/d}' f.txt

sed "/Text/ d;30 q" f.txt
should be faster, especially on huge file IF you discard line after 30. If not previous solution are the best

Related

use sed to delete patterns on a range of lines

I have a large text file that I would like to divide into segments and use sed to delete certain patterns in place. I would like to do this in a single command line using a pipe. For example:
sed -n 1,10p <text file> | sed -i 's/<pattern to remove>//'
The code above attempts to take the first 10 lines of the text file and remove the patterns from the 10 lines in place. The resulting text file should have the first 10 lines modified. The code above doesn't work because the second command after the pipe requires a input file. Please help!
Something like
sed -i '1,10s/pattern//' foo.txt
though for in place editing of a file I prefer ed or perl instead of relying on a non standard sed extension like -i.
This seems to do what you're asking ....
sed -ni '1,10s/pattern//p' file

blank lines in new file written by sed

I used sed '/pattern/d' file > newfile to remove some lines in a text file, but there are lots of blank lines left in the new file.
How can I modify the command to avoid the blank lines?
sed '/pattern/d; /^$/d' file > newfile
There is some good discussion about regular expressions for deleting empty lines in a file in this Stack Overflow post
sed '/^$/d' file >newfile
...will do it for you.
The command that you use will delete all the lines you want to delete, but it leaves the blank lines that are already there in the original file in place. To delete these too, simply apply a second filter to the input:
$ sed -e '/pattern/d' -e '/^[:blank:]*$/d' <file >newfile
The second filter will remove lines that are empty, or that contains only whitespace characters (i.e., that are "blank" in their appearance).

Is there a command in Linux/Unix to comment out multiple lines at once?

I am looking for command or a shortcut to comment out multiple lines at once in Linux/Unix. For example, in a file we have 200 lines and I want to comment first 100 lines only. I know we can use # before every line to make it as comment. But is there a way to do all 100 lines at once and save time?
Sure, using sed:
sed -i '1,100s/^/# /' file
-i makes sed modify file in-place; 1,100 is the range of lines 1–100 in the file; “s” is for “substitute”; ^ is the beginning of the line; # is its replacement.

Quickest way to remove 70+ strings from a file?

I have 70+ strings I need to find and delete in a file. I need to remove the entire line in the file that the string appears in.
I know I can use sed -i '/string to remove/d' fileA.txt to remove them one at a time. However, considering I have 70+, it will take some time doing it this way.
Is there a way I can put these 70+ strings in a file and have sed go through them one by one? Or if I create a file containing the strings, is there a way to compare the two files so it removes any line from fileA that contains one of the strings?
You could use grep:
grep -vf file_with_words.txt file.txt
where file_with_words.txt would be the file containing the list of words, each word being on a different line and file.txt is the file that you want to remove the lines from.
If your list of words contains regex metacharacters, then tell grep to consider those as fixed strings (if that is what you want):
grep -F -vf file_with_words.txt file.txt
Using sed, you'd need to say:
sed '/word1\|word2\|word3/d' file.txt
or
sed -E '/word1|word2|word3/d' file.txt
You could use command substitution to construct the pattern too:
sed -E "/$(paste -sd'|' file_with_words.txt)/d" file.txt
but grep is clearly the tool to use in this case.
If you want to do the job in bash, here's how:
search=fileA.txt
queries=queries.txt
while read query
do
sed -i '' "/$query/d" $search
done < "$queries"
where queries.txt looks like
I
want
to
delete
these
lines

Delete the first five characters on any line of a text file in Linux with sed

I need a one-liner to remove the first five characters on any line of a text file. How can I do that with sed?
Use cut:
cut -c6-
This prints each line of the input starting at column 6 (the first column is 1).
sed 's/^.....//'
means
replace ("s", substitute) beginning-of-line then 5 characters (".") with nothing.
There are more compact or flexible ways to write this using sed or cut.
sed 's/^.\{,5\}//' file.dat
awk '{print substr($0,6)}' file

Resources