Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a CSV which has the follwing data

time     t_count       state       hour
_____    ________      _____      _____
  1        10            2          1
  2        12            2          1
  3        30            2          1
  4        12            2          1
  1        9             3          1
  2        18            3          1
  6        27            3          1
  1        18            4          1
  2        30            4          1
  9        27            4          1
.......................................
......................................
  2        30            2          2
  8        18            2          2
  3        36            2          2
  3        6             3          2
  6        19            3          2
  8        28            3          2
  1        19            4          2
  2        36            4          2
  9        30            4          2
.....................................
.....................................
....................................

Likewise I have time and t_count for each state and each hour.I have state form 2 to 99 and hour from 1 to 23.So I need to split this CSV into each CSV for each state and each hour as

 state_2_hour_1.csv
  1        10            2          1
  2        12            2          1
  3        30            2          1
  4        12            2          1



 state_3_hour_1.csv

  1        9             3          1
  2        18            3          1
  6        27            3          1

...

state_2_hour_2.csv
      2        30            2          2
      8        18            2          2
      3        36            2          2

and so on.Any help is appreciated.

share|improve this question

try this awk command.

awk 'NR>2{filename="state_"$3"_hour_"$NF".csv"; print $0 > filename}' input.csv
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.