Help me with file conversion [Shell Script]

Hello Group,

I request your help with a shell script for filter an ascii file (this is small piece of the file):

09/24/2009,00:00,1.0268,1.0268,1.0249,1.0250,518
09/24/2009,01:00,1.0251,1.0261,1.0249,1.0259,424
09/24/2009,02:00,1.0258,1.0272,1.0258,1.0269,372
09/24/2009,03:00,1.0270,1.0276,1.0266,1.0270,389
09/24/2009,04:00,1.0269,1.0277,1.0267,1.0268,488
09/24/2009,05:00,1.0267,1.0275,1.0262,1.0269,575
09/24/2009,06:00,1.0270,1.0271,1.0248,1.0248,670
09/24/2009,07:00,1.0249,1.0254,1.0238,1.0238,1314
09/24/2009,08:00,1.0239,1.0259,1.0238,1.0240,1111
09/24/2009,09:00,1.0241,1.0245,1.0219,1.0220,1291
09/24/2009,10:00,1.0221,1.0242,1.0207,1.0236,1389
09/24/2009,11:00,1.0237,1.0241,1.0221,1.0221,1049
09/24/2009,12:00,1.0220,1.0235,1.0210,1.0216,1189
09/24/2009,13:00,1.0215,1.0249,1.0211,1.0237,1125
09/24/2009,14:00,1.0239,1.0290,1.0238,1.0268,1837
09/24/2009,15:00,1.0269,1.0292,1.0264,1.0278,1290
09/24/2009,16:00,1.0277,1.0282,1.0262,1.0273,874
09/24/2009,17:00,1.0272,1.0322,1.0268,1.0319,1080
09/24/2009,18:00,1.0318,1.0320,1.0299,1.0300,674
09/24/2009,19:00,1.0301,1.0305,1.0291,1.0298,636
09/24/2009,20:00,1.0299,1.0304,1.0292,1.0293,220
09/24/2009,21:00,1.0295,1.0298,1.0282,1.0293,332
09/24/2009,22:00,1.0292,1.0299,1.0291,1.0298,183
09/24/2009,23:00,1.0297,1.0315,1.0290,1.0296,543
09/25/2009,00:00,1.0295,1.0319,1.0290,1.0310,669

The second field above correspond to the hour and I need only the hours between 12:00 and 22:00 and discard the others rows.

I have Ubuntu in my computer

I really appreciate it your help

Sincerely,

Carlos

Something like this?

awk -F",|:" '$2 > 11 && $2 < 23' file
1 Like

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Ad hoc:

#! /bin/bash

cat ascii.file | \
while read LINE
do
  TIME=$( echo $LINE | awk -F ',' '{ print $2 }' | sed 's/://' )
  if [ $TIME -ge 1200 ] && [ $TIME -le 2200 ]
  then
    echo "$LINE"
  fi
done

exit 0
1 Like