sed or awk for arithmetic task


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed or awk for arithmetic task
# 1  
Old 02-11-2009
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  
Old 02-11-2009
Code:
nawk 'BEGIN {RS=FS=""} {print $1, NF-1}' myFile

# 3  
Old 02-11-2009
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  
Old 02-11-2009
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  
Old 02-12-2009
Quote:
Originally Posted by marcelino
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.
# 6  
Old 02-12-2009
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
# 7  
Old 02-12-2009
summer_cherry awk and perl script worked perfectly as-is... I just need to understand how you did it...thanks...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Task grep and awk commands

Hi everyone, I have a question, please help me. awk -F: '$3<75' /etc/passwd grep -ir nashorn ./ | grep "^*\.java" what do these commands do ? Thanks (2 Replies)
Discussion started by: burak171
2 Replies

2. UNIX for Beginners Questions & Answers

Conditional Arithmetic in [g]awk

I am having a difficult time getting an awk one-liner to work correctly that runs a mathematical operation upon values in a field when matching a given criteria. I would like to subtract 1 from every value in field $6 that is greater than 12. In this particular case it is only a constant of... (3 Replies)
Discussion started by: jvoot
3 Replies

3. Shell Programming and Scripting

Using awk to do arithmetic operation

Hi, I've this following text file FileVersion = 1.03 Filetype = meteo_on_curvilinear_grid TIME = 0 hours since 2016-10-03 12:00:00 +00:00 -6.855 -6.828 -6.801 -6.774 -6.747 -6.719 -6.691 -6.663 -6.634 -6.606 -6.577 -6.548 -6.519 -6.489 TIME = 0 hours since... (2 Replies)
Discussion started by: xisan
2 Replies

4. UNIX for Dummies Questions & Answers

Basic arithmetic operation with awk?

input: Name|Operation rec_10|1+2+2- Output: rec_10|1 Basically I am trying to calculate the result of "the path" in $3 where the operators follow the number and not preceding them like we normally do: rec_10: +1+2-2=1 But I realise (I am sure there is a good reason for that) that awk... (7 Replies)
Discussion started by: beca123456
7 Replies

5. Shell Programming and Scripting

PAssing variables to awk arithmetic

Hi all, I am wanting to pass variables from a file to an awk arithmetic formula. When I use the formula with the value it works well. As soon as I make these variables I get an inf (infinity) response. I can certainly echo the variables back and they look correct. My googling for answers has... (3 Replies)
Discussion started by: gafoleyo73
3 Replies

6. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

7. Shell Programming and Scripting

arithmetic from csh variable passed to awk

I have the following code in a csh script I want to pass the value of the variable sigmasq to the awk script so that I can divide $0 by the value of sigmasq grep "Rms Value" $f.log \ | awk '{ sub(/*:*\.*/,x); \ print... (2 Replies)
Discussion started by: kristinu
2 Replies

8. Shell Programming and Scripting

Arithmetic operation with awk

I have output like following in a file usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:23:58 usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:24:04 (9 Replies)
Discussion started by: fugitive
9 Replies

9. Shell Programming and Scripting

sed in while loop producing arithmetic output

Hi all Just wondering if someone can help me with this. I'm trying to write a script that processes the output of another file and prints only the lines I want from it. This is only the second script I have written so please bare with me here. I have referred to the literature and some of the... (3 Replies)
Discussion started by: javathecat
3 Replies

10. Shell Programming and Scripting

comment and Uncomment single task out of multiple task

I have a file contains TASK gsnmpproxy { CommandLine = $SMCHOME/bin/gsnmpProxy.exe } TASK gsnmpdbgui { CommandLine = $SMCHOME/bin/gsnmpdbgui.exe I would like to comment and than uncomment specific task eg TASK gsnmpproxy Pls suggest how to do in shell script (9 Replies)
Discussion started by: madhusmita
9 Replies
Login or Register to Ask a Question