Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2012
Registered User
 

Join Date: Sep 2006
Posts: 88
Thanks: 2
Thanked 1 Time in 1 Post
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 02:35 PM.. Reason: Use code tags, please.
Sponsored Links
    #2  
Old 02-04-2012
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 8,506
Thanks: 67
Thanked 401 Times in 390 Posts

Code:
awk '/^[0-9]/ && length($1)<2 {next} {print} ' inputfile

using awk.
The Following User Says Thank You to jim mcnamara For This Useful Post:
ramky79 (02-04-2012)
Sponsored Links
    #3  
Old 02-04-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts

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
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts
@ramky79: A little tweak to your attempt. Add space after [0-9] - "[0-9] ".

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

Sponsored Links
    #5  
Old 02-04-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Using grep...

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

--ahamed
Sponsored Links
    #6  
Old 02-05-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
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]
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to add single digit in front of the word and line in the file. krbala1985 Shell Programming and Scripting 6 04-14-2011 12:26 PM
single line command to delete a 6 months old file wtolentino UNIX for Dummies Questions & Answers 6 07-29-2009 02:23 PM
Single digit date to double digit date. villain41 Shell Programming and Scripting 3 02-02-2009 03:53 AM
delete a single space in a line Satyak Shell Programming and Scripting 1 10-31-2008 07:01 AM
Append 0 for single digit entered from command line namishtiwari UNIX for Dummies Questions & Answers 2 08-24-2007 05:01 AM



All times are GMT -4. The time now is 04:26 AM.