how to delete the line if the first letter is a single digit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to delete the line if the first letter is a single digit
# 1  
Old 02-04-2012
how to delete the line if the first letter is a single digit

Hi,
I'm trying to acheive the following, I have a dat file in which i have several addresses, If the address starts with a single digit then i have to delete the line,
if it starts with 2 or more digits then i have to keep the line

Here is a sample of my file:
Code:
377 CARRER DE LA DIPUTACIÓ	BARCELONA	CATALUNYA	08013	ESP	
77 GREEN STREET	HIGH WYCOMBE	BUCKINGHAMSHIRE	HP11 2	GBR
2 KREUZWEG	ERSIGEN	BERN	3423	CHE	
3 MUNCHPLATZ	10. BEZIRK-FAVORITEN	WIEN	1100	AUT

Here is what i want in my output file:
Code:
377 CARRER DE LA DIPUTACIÓ	BARCELONA	CATALUNYA	08013	ESP	
77 GREEN STREET	HIGH WYCOMBE	BUCKINGHAMSHIRE	HP11 2	GBR

I have tried the following regex:
sed -e /^[0-9]/d -- this is deleting all lines.

Last edited by Scott; 02-04-2012 at 03:35 PM.. Reason: Use code tags, please.
# 2  
Old 02-04-2012
Code:
awk '/^[0-9]/ && length($1)<2 {next} {print} ' inputfile

using awk.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-04-2012
Code:
grep -E '^[0-9]{2}' infile

Code:
sed '/^[0-9][0-9]/!d' infile

Code:
awk '/^[0-9]{2}/' infile

whitespace proof:
Code:
awk '$1~/[0-9]{2}/' infile1039

# 4  
Old 02-04-2012
@ramky79: A little tweak to your attempt. Add space after [0-9] - "[0-9] ".
Code:
sed '/^[0-9] /d' inputfile

# 5  
Old 02-04-2012
Using grep...
Code:
grep -v "^[0-9] " infile

--ahamed
# 6  
Old 02-05-2012
Then use [ \t], since otherwise this will not work if the whitespace happens to be tab:
Code:
sed '/^[0-9][ \t]/d' inputfile

Code:
grep -v "^[0-9][ \t]" infile

Or to make it also tolerant to whitespace at the beginning - like the last awk example - use ^[ \t]*[0-9][ \t]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking a single digit in a file using Grep

Hi All, I have a file which keeps count based on completion of a certain activity. I am using the following grep command to return a '1' in case the count is zero grep -ic "0" abc_count.txt Now the issue happens when the count is '10', '20' etc .. in these cases as well it returns a... (5 Replies)
Discussion started by: dev.devil.1983
5 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

4. Shell Programming and Scripting

how to add single digit in front of the word and line in the file.

Hi , how to add the single digit to front of the word and front of the lines in the one file with compare pattern file and get digit. like example pattern file pattern.txt pattern num bala 2 raja 3 muthu 4 File Name: chennai.dat muthu is good boy raja is bad boy selvam in super... (6 Replies)
Discussion started by: krbala1985
6 Replies

5. UNIX for Dummies Questions & Answers

single line command to delete a 6 months old file

i had this scenario where i need to delete a file that is 6 months old which is no longer needed. basically the filename is in the format of PCARDDAILYmmddyyyy.txt where the mm is the month, dd is the day, and yyyy is the year. e.g. PCARDDAILY05262009.txt PCARDDAILY05252009.txt ... (6 Replies)
Discussion started by: wtolentino
6 Replies

6. Shell Programming and Scripting

Single digit date to double digit date.

I have a var storing date var=`date` Now the date is returned as Mon Feb 2 00:25:48 PST 2009 Is there any way to check the date field alone ("2" in above case) and if its a single digit then add a prefix 0 to it and store the result in same variable "var" My intention in above case is... (3 Replies)
Discussion started by: villain41
3 Replies

7. Shell Programming and Scripting

month in single digit

hi all, how do i get the month in single digit in ksh script ?? my command for date is : /usr/bin/date +%Om/%Oe/%y | sed 's/ //g' which returns "01/16/09" but i need the output to be "1/16/09" i.e the month without leading zero. thanks in advance. (2 Replies)
Discussion started by: cesarNZ
2 Replies

8. Shell Programming and Scripting

delete a single space in a line

hi i have a line like TL1330000000800 000DE9248737900 08000TS0231DE92 87379AMEX0000.T N 0080000000 00000. if there any single space between strings, i need to delete, if more than one keep the space as it is can some one help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

9. Shell Programming and Scripting

single digit for day and month on date

hi all, how do i format the date command so it displays day and month in single digits i.e 8 instead of 08 ?? am using the command (in a ksh) : date +%D output i get is 10/08/08 thanks in advance. (5 Replies)
Discussion started by: cesarNZ
5 Replies

10. UNIX for Dummies Questions & Answers

Append 0 for single digit entered from command line

I have a script like this-- #!/bin/ksh echo "To pad a 0 before digits from 1-9" for i in $* do echo $i | sed 's//'0'/g' done I run this script as ksh name 1 2 23 34 The output should be 01 02 23 34 Help me in modifying this script. Thanks Namish (2 Replies)
Discussion started by: namishtiwari
2 Replies
Login or Register to Ask a Question