LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2013, 08:34 AM   #1
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Rep: Reputation: Disabled
show line have repated # four time by awk


File
1111,2#4#5
2222,3#7#9#8#9
3333,2#7#8
4444,5#7#8#8#

Output should be
2222,3#7#9#8#9
4444,5#7#8#8#

Due to # repated 4 time
Thanks in advance for your support
 
Old 06-20-2013, 08:53 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,137

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Code:
awk '/#.*#.*#.*#/' <foo
2222,3#7#9#8#9
4444,5#7#8#8#
 
1 members found this post helpful.
Old 06-20-2013, 09:11 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '/(.*#){4}/' file
 
1 members found this post helpful.
Old 06-20-2013, 09:24 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
OP asked for an awk solution and got two of them.

As a learning exercise I tried to make a sed solution
and failed, probably due to "greedy" matching.
Code:
sed '/#.*#.*#.*#/p' $InFile >$OutFile
Please show how to do this with sed and/or grep.

Daniel B. Martin
 
Old 06-20-2013, 09:41 AM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,137

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Here's Perl for free:

Code:
perl -ne '/#.*#.*#.*#/ && print' <foo
2222,3#7#9#8#9
4444,5#7#8#8#
 
Old 06-20-2013, 11:22 AM   #6
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Code:
awk '/#.*#.*#.*#/' <foo
2222,3#7#9#8#9
4444,5#7#8#8#

Many thanks for you

But if there are other column such as below and need to apply only on $2
By use
Awk -F,

1111,2#4#5
2222,3#7#9#8#9,sample#only
3333,2#7#8
4444,5#7#8#8#,sample##
 
Old 06-20-2013, 11:24 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sed:
Code:
sed -rn '/(.*#){4}/p' file
Grep:
Code:
grep -E '(.*#){4}' file
Ruby:
Code:
ruby -ne 'puts $_ if /(.*#){4}/' file
 
2 members found this post helpful.
Old 06-20-2013, 11:25 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I believe everyone has provided several examples and alternatives ... maybe you should have a go and see if you can extend one to solve your new criteria.
 
Old 06-20-2013, 11:26 AM   #9
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Code:
awk '/(.*#){4}/' file
Dear grail
It doesn't work with me not show any result
I like becuse it is very short command is there any missed
Attached Thumbnails
Click image for larger version

Name:	Screenshot_2013-06-20-19-23-19-1.png
Views:	14
Size:	45.4 KB
ID:	12757  
 
Old 06-20-2013, 11:28 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What version and type of awk are you using?

Mine is:
Code:
$ awk --version
GNU Awk 4.1.0, API: 1.0 (GNU MPFR 3.1.2, GNU MP 5.1.2)
 
Old 06-20-2013, 11:32 AM   #11
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
GNU Awk 3.1.7
Copyright (C) 1989, 1991-2009 Free Software Foundation.

I think i have to update ..many thanks to you all
 
Old 06-20-2013, 11:39 AM   #12
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by phpshell View Post
Many thanks for you

But if there are other column such as below and need to apply only on $2
By use
Awk -F,

1111,2#4#5
2222,3#7#9#8#9,sample#only
3333,2#7#8
4444,5#7#8#8#,sample##

awk -F, '$2~/#.*#.*#.*#/' file
2222,3#7#9#8#9
4444,5#7#8#8#
 
Old 06-20-2013, 12:01 PM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by phpshell View Post
GNU Awk 3.1.7
Copyright (C) 1989, 1991-2009 Free Software Foundation.

I think i have to update ..many thanks to you all
To run with back-level awk, try this variation of grail's code ...
Code:
awk  --re-interval '/(.*#){4}/' $InFile >$OutFile
Daniel B. Martin
 
Old 06-20-2013, 07:32 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
2 Perl variations

Code:
# match comma then any single char followed by # up to 4 x #
perl -ne '/,.#.#.#.#/ && print' t.t

# as above, but single char must be single digit
perl -ne '/,[0-9]#[0-9]#[0-9]#[0-9]#/ && print' t.t
 
Old 06-23-2013, 07:58 AM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If all you want to do is print the matching lines, then grep is almost certainly the most efficient option, as posted by grail before. That's exactly what it was designed to do, after all. If you don't have gnu grep with extended regex (which supports the {..} interval matching), then you just have to build the entire expression by hand.

Code:
grep -E '([0-9]#){4}' input.txt
grep '[0-9]#[0-9]#[0-9]#[0-9]#' input.txt
awk would only have to be used if the matching pattern needs to appear in a specific column, and there's a chance that the same pattern could occur in other columns as well. And even then a more specific regex for grep could likely be designed instead.

I also think it's a not a good idea to use ".*" in this kind of match. Since "*" is greedy, you run a real risk of it consuming more than you want it to. Good regular expression design is careful to match only the intended pattern.


And just to clarify, gnu awk from version 4 interprets {..} by default. For versions before that you need to use the --re-interval option to enable it. Some other versions of awk may not support it at all (e.g. mawk).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Calculate time with awk with an exception for the last line jostber Programming 9 04-17-2013 08:17 AM
[SOLVED]Wierd AWK behavior / AWK not reading first line. Involar Linux - Newbie 9 11-28-2012 10:53 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 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