The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parse an XML task list to create each task.xml file MissI Shell Programming and Scripting 3 11-11-2008 02:20 PM
comment and Uncomment single task out of multiple task madhusmita Shell Programming and Scripting 9 06-18-2008 10:42 AM
Arithmetic Operators filda UNIX for Dummies Questions & Answers 0 06-13-2008 01:55 AM
Can I use wc -l with arithmetic expression? lalelle Shell Programming and Scripting 3 08-11-2007 07:44 PM
arithmetic in ksh amon Shell Programming and Scripting 12 02-05-2007 02:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-11-2009
marcelino marcelino is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 4
sed or awk for arithmetic task

I have file with this type of format

01.02.09 08:30
bob
jill
mark

01.04.09 07:00
bob
jill
mark
tom

I want to count the names after the date /ime line (01.02.09 08:30) and add that number after the time like this

01.02.09 08:30 3
01.04.09 07:00 4

I don't care about the names after the count so I can leave them or strip the out with sed if it simplifies the process. I could also redirect the output of date/time and count to second file if it makes it simpler. The only thing I really need is the date/time and number on the same line.

What is the best tool to accomplish this and can someone provide an example?
  #2 (permalink)  
Old 02-11-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk 'BEGIN {RS=FS=""} {print $1, NF-1}' myFile

  #3 (permalink)  
Old 02-11-2009
marcelino marcelino is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 4
Almost it

Nice...

I had to add space to "" and change NF-1 to NF-2 to get accurate count for all lines except the last and first entry. NF-1 gives the correct output for the last line count only. I have 9,000 lines so I having the last and first wrong is not significant issue.

The next problem is that output only has the time in the line like this:

10:00 4
11:00 6

I need the date in the output. Currently looking through the nawk manual to figure out how to combine time and date into output. Any quick suggestions? BTW, running nawk 1.3.3 from Ubuntu 8.10
  #4 (permalink)  
Old 02-11-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
input:

Code:
01.02.09 08:30
bob
jill
mark 

01.04.09 07:00
bob 
jill 
mark
tom

01.02.09 09:30
bob
jill
mark 
hj
gh
fm

01.04.09 10:00
bob 
jill 
mark
tom
aaa
bbb
ccc

output:

Code:
01.02.09 08:30 3
01.04.09 07:00 4
01.02.09 09:30 6
01.04.09 10:00 7

code:

Code:
#!/usr/bin/perl
$/="\n\n";
open FH,"<a.txt";
while(<FH>){
	my @temp=split("\n",$_);
	print $temp[0]," ",$#temp,"\n";
}


Code:
awk '/[0-9]*\.[0-9]*\.[0-9]* [0-9][0-9]:[0-9][0-9]/{
	a=$0
	next
}
/^ *$/{
	print a" "n
	n=0
	next
}
{
 n++
}
' a.txt


Last edited by summer_cherry; 02-11-2009 at 11:54 PM..
  #5 (permalink)  
Old 02-12-2009
marcelino marcelino is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 4
summer_cherry awk and perl script worked perfectly as-is... I just need to understand how you did it...thanks...
  #6 (permalink)  
Old 02-12-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by marcelino View Post
Nice...

I had to add space to "" and change NF-1 to NF-2 to get accurate count for all lines except the last and first entry. NF-1 gives the correct output for the last line count only. I have 9,000 lines so I having the last and first wrong is not significant issue.

The next problem is that output only has the time in the line like this:

10:00 4
11:00 6

I need the date in the output. Currently looking through the nawk manual to figure out how to combine time and date into output. Any quick suggestions? BTW, running nawk 1.3.3 from Ubuntu 8.10
Hmmm.. very strange...
mar.txt:

Code:
01.02.09 08:30
bob
jill
mark

01.04.09 07:00
bob
jill
mark
tom

nawk 'BEGIN {RS=FS=""} {print $1, NF-1}' mar.txt

output:

Code:
01.02.09 08:30 3
01.04.09 07:00 4

Looks fine to me. Post your input file (or a portion of it) using vB Code tags.
  #7 (permalink)  
Old 02-12-2009
marcelino marcelino is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 4
response

Using the same data you had example, I got the following:

nawk 'BEGIN {RS=FS=""} {print $1, NF-1}' data.txt
0 27
0 31

Adding space to ""

$ nawk 'BEGIN {RS=FS=" "} {print $1, NF-1}' data.txt
01.02.09 0
08:30 4
07:00 4

Changing to NF-2

$ nawk 'BEGIN {RS=FS=" "} {print $1, NF-2}' data.txt
01.02.09 -1
08:30 3
07:00 3
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0