Bash Script Generate Output Value "0"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script Generate Output Value "0"
# 1  
Old 03-08-2018
Bash Script Generate Output Value "0"

Hi All

could u please help me about my query problem below :

Code:
cat filesname | awk -F"[ |]" '{cc[substr($2,1,8)]++ } END {for ( i in cc ) print i,cc[i]}' | sort -nk1

will shown output below :

Code:
12:02:06 4
12:02:08 3
12:02:12 8
12:02:17 2


my expectation result for value "0" also will print also to output files.

expectation like below :

Code:
12:02:06 4
12:02:07 0
12:02:08 3
12:02:09 0
12:02:10 0
12:02:11 0
12:02:12 8
12:02:13 0
12:02:14 0
12:02:15 0
12:02:16 0
12:02:17 2

aprreciate for any one can help my problem above.

Thanks
Fajar


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-08-2018 at 04:22 AM.. Reason: Added CODE tags.
# 2  
Old 03-08-2018
It would help if you could post an extract from filesname, containing some of the lines representing the issue.

Also, for reference, awk does not need the help of cat to read filesname.

Instead of:
Code:
cat filesname | awk ...

do:
Code:
awk ... filesname

# 3  
Old 03-08-2018
Quote:
Originally Posted by fajar_3t3
Hi All

could u please help me about my query problem below :

Code:
cat filesname | awk -F"[ |]" '{cc[substr($2,1,8)]++ } END {for ( i in cc ) print i,cc[i]}' | sort -nk1

will shown output below :

Code:
12:02:06 4
12:02:08 3
12:02:12 8
12:02:17 2


my expectation result for value "0" also will print also to output files.

expectation like below :

Code:
12:02:06 4
12:02:07 0
12:02:08 3
12:02:09 0
12:02:10 0
12:02:11 0
12:02:12 8
12:02:13 0
12:02:14 0
12:02:15 0
12:02:16 0
12:02:17 2

aprreciate for any one can help my problem above.

Thanks
Fajar


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!
So you are extracting timestamps from the file and counting how many times each timestamp appears? But you want to add in missing timestamps to the list? That is, timestamps that don't actually appear in the file and therefore have the count of zero?

Do you know what the first and last timestamps are going to be before you begin?

Andrew
# 4  
Old 03-08-2018
Quote:
Originally Posted by apmcd47
So you are extracting timestamps from the file and counting how many times each timestamp appears? But you want to add in missing timestamps to the list? That is, timestamps that don't actually appear in the file and therefore have the count of zero?

Do you know what the first and last timestamps are going to be before you begin?

Andrew
Hi Andrew

Yes correct, for value 0 currently missing in output files.

for the first time stamp begin 00:00:00 and last time stamp 23:59:59

Thanks
Fajar
# 5  
Old 03-08-2018
So you want 86400 lines all but four of which have a zero count?
# 6  
Old 03-08-2018
Try this:

Code:
awk -F"[ |]" '
{cc[substr($2,1,8)]++ }
END {
 for(h=0;h<24;h++) for(m=0;m<60;m++) for(s=0;s<60;s++) {
    i=sprintf("%02d:%02d:%02d",h,m,s)
    print i,cc[i]+0
}}' filesname

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-09-2018
Recent bash? Try
Code:
echo -e "x "{00..23}:{00..59}:{00..59}"\n" | awk '{CNT[$2]++} END {for (c in CNT) print c, CNT[c]-1}' - file | sort
.
.
.
12:02:05 0
12:02:06 2
12:02:07 0
12:02:08 3
12:02:09 0
12:02:10 0
12:02:11 0
12:02:12 3
12:02:13 0
.
.
.

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

What is the right way to redirect script output use ">" or ">>" ?

Which one of the following are more accurate and why? nohup myScript.sh 1>nohup_$(date +%Y%m%d%H%M%S).out 2>&1 & nohup myScript.sh 1>>nohup_$(date +%Y%m%d%H%M%S).out 2>&1 & nohup myScript.sh >nohup_$(date +%Y%m%d%H%M%S).out 2>&1 & nohup myScript.sh >>nohup_$(date +%Y%m%d%H%M%S).out 2>&1 &... (3 Replies)
Discussion started by: kchinnam
3 Replies

3. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

4. Shell Programming and Scripting

How to insert "#!/bin/bash" to output file

Hi Guys, I want to put or insert #!/bin/bash to my output file named outputfile.sh. Please need some help. inputfile.txt 1111111111 2222222222 3333333333 script: awk -f script.awk inputfile.txt > outputfile.sh DESIRED OUTPUT: (1 Reply)
Discussion started by: pinpe
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

9. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies

10. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies
Login or Register to Ask a Question