Kay
Kay

Reputation: 2077

Remove the lines starting with a character in shell

I have dataset as:

file.txt
de
fds
fds
a
sa
1
2
3
1
}

I would like to delete all the lines starting with characters or special characters. So my outfile is:

out.txt
1
2
3
1

I could do it manually with 'sed', but I am looking for a suitable command for this.

my code:

sed -i '/d//g' file.txt
sed -i '/f//g' file.txt
sed -i '/a//g' file.txt
sed -i '/s//g' file.txt
sed -i '/}//g' file.txt

Upvotes: 1

Views: 1467

Answers (2)

Weike
Weike

Reputation: 1270

just keep lines starting with a number:

$ sed -i.bak -r '/^[0-9]/!d' filename

or delete lines starting with specific characters:

$ sed -i.bak -r '/^[dfas}]/d' filename

Upvotes: 2

RaphaMex
RaphaMex

Reputation: 2839

Use grep with -E option for regex (or egrep in short):

grep -E "^[0-9].*" file.txt

Upvotes: 2

Related Questions