LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-02-2012, 10:42 AM   #1
oldfogey
LQ Newbie
 
Registered: Jun 2012
Location: New York City
Posts: 5

Rep: Reputation: Disabled
awk commands/script to extract lines with a specific format


Hi. I'm trying to extract lines from a file, each of which are of a certain format. I can write awk commands to do parts of this as individual steps but I'd like to link everything together so I don't have to issue a sequence of individual commands.

Here's a sample portion of the input file:

F9JcQEcS21=SQZK6if6mfnA
pc\sender4
Wed12:25:17
abc124
ba1
clp1491
N18471
Tam-port
dd
e.44
flash9
1c-64-94-98-87-11

I'd like to extract all lines from the input file that:

1) only contain alphanumeric characters (all alpha characters a-z being lowercase)
2) the beginning of each line must have a minimum of 2 alpha characters
3) the last character must be a number.

So the properly extracted lines from the sample above would be:

abc124
ba1
clp1491
flash9

Thanks in advance for your suggestions!
 
Old 10-02-2012, 12:16 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Based on what you show as desired output, it sounds as if you want to output lines that start with a minimum of two lower case letters, end with digits, and contain only lower case letters and digits. Using your example data as input, this command gets the desired output:

Code:
gawk --posix '/^([a-z]{2,})([0-9]+)$/ { print $0 }'
 
1 members found this post helpful.
Old 10-02-2012, 12:26 PM   #3
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
You can also do this with grep-

Code:
egrep "^([a-z]){2}[a-z]*[0-9]*[0-9]$"
then direct the output to a file or xargs.
 
Old 10-02-2012, 12:34 PM   #4
oldfogey
LQ Newbie
 
Registered: Jun 2012
Location: New York City
Posts: 5

Original Poster
Rep: Reputation: Disabled
Fantastic! You guys are tremendous!

Thank you.
 
Old 10-02-2012, 01:39 PM   #5
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by kakaka View Post
Based on what you show as desired output, it sounds as if you want to output lines that start with a minimum of two lower case letters, end with digits, and contain only lower case letters and digits. Using your example data as input, this command gets the desired output:

Code:
gawk --posix '/^([a-z]{2,})([0-9]+)$/ { print $0 }'
This would produce the example output, but the description given would also allow digits followed by letters - lines like abc123def99

If the description's correct and the examples were missing such a case, you would need
Code:
gawk --posix '/^[a-z]{2}[a-z0-9]*[0-9]+$/ { print $0 }'
 
Old 10-02-2012, 01:50 PM   #6
oldfogey
LQ Newbie
 
Registered: Jun 2012
Location: New York City
Posts: 5

Original Poster
Rep: Reputation: Disabled
You're correct. A string such as abc123def99 would not be desired in the output.

The output must be (minimum 2) alphas followed by (at least 1) numeric.

Thanks for the catch!
 
  


Reply

Tags
awk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk - summing over specific lines ilukacevic Programming 7 03-31-2011 04:33 AM
How to extract lines from file using AWK keenboy Linux - General 7 08-05-2010 08:29 AM
deleting lines from a file with specific pattern using AWK gandhigaurav1986 Programming 12 06-08-2010 02:08 AM
Get all lines containing 23 specific words with AWK cgcamal Programming 3 11-05-2008 10:51 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration