finding data in a file

Hello,

I am new to shell programming, and Unix development overall.

I have a following text file with the following contents:

..
Sunny
Monday: x
Tuesday: x
Wednesday: x
Thursday: x
Friday: x
Cloudy
Monday: x
Tuesday: x
Wednesday: x
Thursday: x
Friday: x
Precipitation
Monday: x
Tuesday: x
Wednesday: 50%
Thursday: x
Friday: x
..

I need to extract the Precipitation for Wednesday (I just need the 50%). Position of Precipitation is not always the same in the file. I have tried to use grep and awk, but have not been successful so far, probably because I haven't used awk before now.

Does anyone have any ideas? If so, please help.

Thank you in advance,

Selma

try something like :
IFS=:
awk -F '{print $2}' input file | grep -v 'x'
should work!!
moxxx68

Thanks for the quick reply. Could you please explain it a little bit more? I do not see where in this command does it say "search for 'precipitation'".

-> Wednesday: 50%
-> Thursday: x

awk -F '{print $2}' input file = recupere the second word (50% and x)

|grep -v 'x' = exlude the character "x"

Also you could used :
awk -F '{print $2}' input file | grep %
you recupere only the %

It is the same result

Yes, that's how I understood it as well. But this doesn't help me with my problem. Xs were there just in place of some other numbers, I don't acutally get Xs in the file I need to parse, so if for example I have:

Sunny
Monday: 10
Tuesday: 15
Wednesday: 53
Thursday: 57
Friday: 8
Cloudy
Monday: 35
Tuesday: 7
Wednesday: 95
Thursday: 68
Friday: 98
Precipitation
Monday: 15
Tuesday: 29
Wednesday: 50
Thursday: 26
Friday: 1
...

then the suggested command would return all of the numbers in the second column if I understand this correctly.
The point here is that I need to find the Precipitation category first, and then get data for Wednesday only. Can you help me with that?

Sorry if my first example was confusing.

Thank you.

try
IFS=:
grep -nA7 'Precipitation' | awk -F '/Wednesday/{print $2)
should work..
or alternately
grep -nA7 'Precipitation' | awk '-F /Wednesday/{print, "Precipitation:" $2)
hope that solves the problem
moxxx68

I'd personally use sed

#!/bin/sed -nf

/Precipitation/,/^[^:]*$/ {
   /Wednesday/p
}

Then invoke with
foo.sed filename_here

Cheers
ZB

dear Zazzybob
my sed is a little on the rusty side:
could you please clarify a little.....
/precipitation/,/^[^:]$/
" /^[^:]
$/" ? (confused!)
also how does the curly bracket play into the syntax..
if you could give an example using curly brackets I would appreciate it..
this is as far as I go with curly brackets and sed..
ex,. sed -e '/foo/{ h; d; }' -e 'N{g}'
other wise i don't know how to apply it the way you did..
moxxx68
ps. I know its a little off the subject but maybe both I and the poster could learn from an example..

It may be easiest if I explain my example

#!/bin/sed -nf

/Precipitation/,/^[^:]*$/ {
   /Wednesday/p
}

/Precipitation/,/^[^:]*$/

This says find anything between the word Precipitation, and the first line that doesn't contain a colon (therefore, this allows us to grab just the "Precipitation" block plus the next header). Then, the curly braces allow us to group together any commands we want sed to execute when the condition is met. I suppose you could analogise this with awks pattern/action behaviour. So all I want to do is print the line(s) that contain "Wednesday". I have formed the script to allow for a little bit of "free-form" within the file. With some of the examples given in this thread, they rely on a rigid and strict file format.

Cheers
ZB

cheers

$ awk '/Precipitation/,/Wednesday/{x=$NF}END{print x}' file1
50

These were all very helpful, my problem is solved, and I learned something :slight_smile:

Thank you all very much for your help.

Best regards,

Selma