0

I have a file, which has multiple lines.

For example:

a
ab#
ad.
a12fs
b
c
...

I want to use sed or awk delete the line, if the line include symbols or numbers. (For example, I want to delete: ab#, ad., a12fs.... lines) or in another words, I just want to keep the line which include [a-z][A-Z] .

I know how to delete number line,

sed '/[0-9]/d' file.txt

but I do not know how to delete symbols lines.

Or there has any easy way to do that?

4 Answers 4

2

To keep blank lines:

grep '^[[:alpha:]]*$' file
sed '/[^[:alpha:]]/d' file
awk '/^[[:alpha:]]*$/' file

To remove blank lines:

grep '^[[:alpha:]]+$' file
sed -E -n '/^[[:alpha:]]+$/p' file
awk '/^[[:alpha:]]+$/' file
Sign up to request clarification or add additional context in comments.

Comments

1

grep works well too and is even simpler: just do the reverse: keep the lines that interest you, which are way easier to define

grep -i '^[a-z]*$' file.txt

(match lines containing only letters and empty lines, and -i option makes grep case-insensitive)

to remove empty lines as well:

grep -i '^[a-z]+$' file.txt

caution when using Windows text files, as there's a carriage return at the end of the line, so nothing would match depending on grep versions (tested on windows here and it works)

but just in case:

grep -iP '^[a-z]*\r?$'

(note the P option to enable perl expressions or \r is not recognized)

Comments

1

You can use this sed:

sed '/^[A-Za-z0-9]\+$/!d' file

(OR)

sed '/[^A-Za-z0-9]/d' file

Comments

1
$ awk '!/[^[:alpha:]]/' file.txt
a
b
c

6 Comments

!/[^[:alpha:]]/ means "NOT containing chars that are NOT alphabetic" which is a double negative and so best avoided if at all possible.
@Ed Morton, I see in your answer how you handled an awk solution compared to this one and your method certainly makes sense. That said, how can this solution be problematic and or could you point me in a direction I could read about why I should avoid using a double negative. Thanks.
Double negatives are just hard to understand, whether it's in a programming language or in spoken language, and the negative of a negative is a positive (-(-1) = 1) so whenever faced with one writing code you should take the time to figure out what the positive condition is and write that instead. To expand that - never write variables in a negative way (e.g. databaseNotAvailable) as then you're almost guaranteed to have to write double negatives somewhere (e.g. !databaseNotAvailable). Compare to !databaseIsAvailable and databaseIsAvailable). Sorry, I don't know of a good reference.
never write is kinda strong - I really mean avoid writing. There may be times when it makes sense to create a negatively-worded variable, just think about it before you do it.
@Ed Morton, Thanks for taking the time to explain. I had to laugh when you said "or in spoken language" because I almost said... "why I should avoid using a double negative" ... except for what my English teacher told me when I was a child. :) Thanks for all your contributions, I keep learning more from your answers all the time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.