LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2017, 11:20 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
awk - print lines contains only digits


I would like to print lines that contains only digits and the length is equal 5.

I can do that with sed with this command:
Code:
sed -E -n '/^[0-9]{5}$/p' /usr/share/wordlists/rockyou.txt | less
This awk command print lines, length only 5 chars:
Code:
awk 'length($0) ==5' /usr/share/wordlists/rockyou.txt | less
How could I print lines with length only 5 chars and contains only digits?

Thanks your help.
 
Old 09-16-2017, 11:30 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
You'd do it about the same way.

If you want the whole line to be just five digits, no more, no less, and nothing else, then:

Code:
awk '/^[0-9]{5}$/' /usr/share/wordlists/rockyou.txt
If you want the same but limited only to the first column, then:

Code:
awk '$1 ~ /^[0-9]{5}$/' /usr/share/wordlists/rockyou.txt
See "man 7 regex". Of course you might also use [[:digit:]] instead of [0-9] but that depends on your locale a little.
 
1 members found this post helpful.
Old 09-16-2017, 11:45 AM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thank you.
 
Old 09-16-2017, 12:11 PM   #4
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
I tried this command, also working.

Quote:
awk '/^[0-9]{5}$/ {print $0}' /usr/share/wordlists/rockyou.txt | less
The print command optional in this case?
I just learning awk and lots of time I use "print" command
Quote:
{print $0}
....
 
Old 09-16-2017, 12:16 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by freeroute View Post
The print command optional in this case?
Yes. There are some shortcuts in awk. If you have nothing after a selector then a { print; } is implied. And { print; } is short for { print $0; }.

Take a peek at the awk manual pages periodically. It's a reference not a tutorial and looking up things in it will make the language more familiar.

Code:
man awk
Scroll down to the section on "PATTERNS AND ACTIONS"
 
1 members found this post helpful.
Old 09-18-2017, 01:31 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
and of course you can use the logical AND and NOT operators
Code:
awk 'length==5 && ! /[^0-9]/'
Length is 5 and there is not a character that is not 0-9.
Even some builtin functions have a default of $0 (analogue to $_ in perl).
The long form is
Code:
awk 'length($0)==5 && !($0~/[^0-9]/) {print $0}'

Last edited by MadeInGermany; 09-18-2017 at 04:04 AM. Reason: ! has higher precedents than ~ brackets added
 
1 members found this post helpful.
Old 09-18-2017, 02:55 AM   #7
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thanks for the another solution...
 
  


Reply



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 or grep to print only seen once lines magische_vogel Programming 19 10-24-2015 10:43 AM
[SOLVED] Using AWK to print out the first few lines of a txt file mskalak Linux - Newbie 3 07-27-2011 02:58 PM
[SOLVED] awk print only lines {not}containing FS grail Programming 8 04-12-2010 04:41 AM
awk to remove first 3 lines and print remaining $1, $2 fields phyx Linux - General 1 01-10-2007 05:21 PM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:33 PM.

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