I have a html file. I want to remove all lines that do not start with
cat my_file | sed $' s/^[^tr].*// ' | sed '/^$/d'
but it deleted all the lines.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Try this with GNU sed:
sed -n '/^<tr>/p' file
sed '/^<tr>/!d' file
sed -e '/^<tr>/d'
The part between
/ is a regex. The
d command deletes matching lines.
Update: oops, sorry I saw you said NOT. So
sed -e '/^<tr>/!d'
! negates the sense of the match.
If it has to be
sed -ni '/^<tr>/p' file
-i edits the file in-place,
sed to print all lines, the regular expression means to match all line that start (
<tr> and those lines will be printed (
grep -E '^<tr>' file
-E grep interprets extended regular expressions.
awk '/^<tr>/' file
while IFS= read -r l; do [[ "$l" =~ ^\<tr\> ]] && echo $l; done <file
[[ is bashs internal conditional expression. We compare
$l against the regular expression and if it succeded (
&&) we print the line with
Easiest and simplest answer would be:
grep '^<tr>' path/to/file
This will print out the file with only the lines that start with which could be good if you don't want to modify the file directly (like with sed).
Then, if you like what you see in the output you can just print out to a file with
In this case you save some time backing up your file before trying some commands.