how to display some special results with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to display some special results with AWK
# 1  
Old 09-25-2012
how to display some special results with AWK

I have a file like this:
HTML Code:
AAEQGAGNQPQH,,,,160,32,,,,
AAGQDYNSPLH,,712,39,,,,,,
AAGREGGNTEAF,26,,,,,,,,4
AAGSPQH,,,,8,5,,,,
AAKKLGQFYNEQF,4,,6,,,7,,,2
AANSGGRYNEQF,,2747,3120,,,,,,
AAQGGVGGELF,,,,5,,12,36,,
AAQGLAGYYEQY,,25,13,,,,,,
AAQRGNEQF,510,2,,,6,,6,,76
AAQTGENSPLH,,16,16,,,,,,
The separator is ",". I want to display certain records, the number of whose non-null field is more than 4.

It should be have some simple way to do that, but I can't get what I want.

Thank you!
# 2  
Old 09-25-2012
Code:
awk -F, 'gsub(",[^,][^,]*","&") >= 4' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 09-25-2012
Code:
awk -F'[^,]*' 'NF>5' file

or
Code:
sed -n 's/[^,]\{1,\}/&/5p' file

or
Code:
grep -E '([^,]+(,+|$)){5}' file


Last edited by Scrutinizer; 09-25-2012 at 12:28 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-26-2012
Quote:
Originally Posted by Scrutinizer
Code:
awk -F'[^,]*' 'NF>5' file

or
Code:
sed -n 's/[^,]\{1,\}/&/5p' file

or
Code:
grep -E '([^,]+(,+|$)){5}' file

The first code will change the number of field, while the last two would not.

When the separator is tab other than "," , what is the code should be? I should learn hard on the regular expression hard.

Thank you !
# 5  
Old 09-27-2012
Hi, the first expression will not change the number of fields, but use any number of non-commas as field separator, so it would do the reverse of what constitutes a field. To do the same with tabs instead of commas, try:
Code:
awk -F'[^\t]*' 'NF>5' file

For the other two, it might be that you need to use an actual TAB-character, rather than '\t', which is entered like CTRL-V TAB
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 09-27-2012
Quote:
Originally Posted by vgersh99
Code:
awk -F, 'gsub(",[^,][^,]*","&") >= 4' myFile

Hi vgersh,
nice code Smilie
can you please explain ?
# 7  
Old 09-27-2012
vgersh99's solution can be work like this also..

Code:
awk -F, 'gsub(",*,","&") >= 4' file

This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Display mysql results nicely

Hi, perhaps this is a dumb question. I'm running queries on mysql and I'm getting tabbed results like these: mysql> SELECT * from metrics_status WHERE date = '2012-03-30'; <TABLE... (1 Reply)
Discussion started by: erick_tuk
1 Replies

2. Shell Programming and Scripting

Display find results, and pipe to xargs

I have an overnight script which runs across a large directory to repair permissions and ownership. I also have this command output the list of files affected so that cron can email these as a log file. Previously I had the command in the form: find /path/to/files -not -user myname -print -exec... (4 Replies)
Discussion started by: mij
4 Replies

3. Shell Programming and Scripting

Shell script compare all parameters in two files and display results

Hi , I am not familiar with shell programming. I have a requirement like i have two files .I need to compare the two files by comparing each parameter and i should produce 2 outputs. 1)i have around 35 parameters say i have one parameter name called db_name=dcap in one file and... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

4. Shell Programming and Scripting

awk loop: display special characters

Hi everybody; I have a code and this fetches data from first.txt,modify it and outputs it to second.txt file. l awk 'NR>1 {print "l ./gcsw "$1" lt all lset Data="$2" Value "$3}' /home/gcsw/first.txt > /home/gcsw/second.txt this outputs as: l ./gcsw 123 lt all lset Data=456 Value 789 ... (1 Reply)
Discussion started by: gc_sw
1 Replies

5. Shell Programming and Scripting

Display echo results in three column

Dear Friends, I have my command output which displays on one row and values are now scrollable (vertical) 3 pages. How do i display those output in three column so that i no need to scroll? Example: dcadd$cat components 1.Caluculator 2.Diary ... ... 50.Mobile 51.Battery .. ...... (12 Replies)
Discussion started by: baluchen
12 Replies

6. Shell Programming and Scripting

need a little help with results from awk

Hi there all, I am using a line to get some replys from my PS I do ps -ef |awk '{printf $9}' But my result is 1 big line. No spaces between the lines or someting for example:... (2 Replies)
Discussion started by: draco
2 Replies

7. UNIX for Dummies Questions & Answers

Unix file does not display special characters

We have a unix file that contains special characters (ie. Ñ, °, É, ¿ , £ , ø ). When I try to read this file I get a codepage error and the characters are replaced by the # symbol. How do I keep the special characters from being read? Thanks. Ryan (3 Replies)
Discussion started by: Ryan2786
3 Replies

8. UNIX for Dummies Questions & Answers

How to display first 7 char of grep results?

My file contains the following: uat2000.aspclient.active=true uat2001.aspclient.active=true uat2002.aspclient.active=true uat2003.aspclient.active=true uat2004.aspclient.active=false uat2005.aspclient.active=false uat2006.aspclient.active=false uat2007.aspclient.active=false... (8 Replies)
Discussion started by: kthatch
8 Replies

9. Solaris

Top and Prstat display different results for memory

I have a question about the accuracy of prstat. I did a 'prstat -t' and it shows 99% of my memory is occupied by oracle. NPROC USERNAME SIZE RSS MEMORY TIME CPU 194 oracle 343G 340G 99% 86:17.24 56% However, 'top' shows I still have 7762meg of memory free. Memory: 16G real, 7762M... (4 Replies)
Discussion started by: zen03
4 Replies

10. Shell Programming and Scripting

Display special characters

I have a file that evidently has some special characters in it. Is there a Unix command that I can use tp display the file so I can see the octal or hex values? (2 Replies)
Discussion started by: BCarlson
2 Replies
Login or Register to Ask a Question