Code_rocks
Code_rocks

Reputation: 151

Remove the line in the file Which has only number in shell script

I have one file which contain sequence of number in every line. I want to remove the line which has only number

I tried (to no avail):

$ cat -n input_file > output_file

My file contain

1 name
2
3 Age
4
5 state
6 city

i want the output as

1 name
3 Age
5 state
6 city

Upvotes: 0

Views: 41

Answers (3)

J. Chomel
J. Chomel

Reputation: 8395

A simple awk formula would do:

cat input_file | awk ' ($2 != "") { print $N } '

Edit: Cleaner way from Tom's comment

awk ' ($2 != "") { print $0 } ' input_file

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74615

Starting with this file:

name

Age

state
city

You can skip the empty lines and add the numbers like this:

awk 'NF { print NR, $0 }' file

When the line contains any non-blank characters (i.e. anything other than spaces or tabs), print the line number followed by the contents of the line.


If the numbers are in the input file already, you can use this:

awk 'NF > 1' file

This prints any line with more than one field.

Upvotes: 1

emmalg
emmalg

Reputation: 346

The easiest way would be to use grep and look for lines with any characters.

testfile.txt:

1 name
2
3 Age
4
5 State
6 city

Then try:

grep '[a-zA-Z]' testfile.txt
1 name
3 Age
5 State
6 city

Upvotes: 1

Related Questions