Posts

Showing posts with the label awk

Match word of specific length

Image
Clash Royale CLAN TAG #URR8PPP Match word of specific length How do I match a word of a specific length, let's say, five? Given the input file temp of text: temp 1) ci sono quattro mele 2) sentiamoci il 16 ottobre 2018 3) decidiamo il 17 ottabre 2017 4) Manipolo di eroi 5) 17 mele 6) 18 ott 2020 7) una mela e mezza 8) 2 mele If i do: awk '/[[:lower:]]{5}/ {print}' temp I would expect as output the sentence 7) , because is the only one with a word of length 5 ( mezza ). Actually, it returns every line with a word of length equal or superior than 5 . 7) 5 mezza 5 This behavior is not compatible with any source of information I consulted: The construct {n} should match exactly n times. At this point, I am afraid I am missing something obvious. {n} n Possible duplicate of Why does this regex with no special character match a longer string? – tripleee 1 hour ago ...

Split CSV by column value, and keep header

Image
Clash Royale CLAN TAG #URR8PPP Split CSV by column value, and keep header This has been asked many times before but I simply can't implement the solutions properly. I have a large csv named 2017-01.csv, with a date column (it's the second column in the file) and I am splitting the file by date. The original file looks like: date 2017-01-01 2017-01-01 2017-01-01 2017-01-02 2017-01-02 2017-01-02 After the split, 2017-01-01.csv looks like 2017-01-01 2017-01-01 2017-01-01 and 2017-01-02.csv looks like 2017-01-02 2017-01-02 2017-01-02 The code I am using is awk -F ',' '{print > (""$2".csv")}' 2017.csv Everything works fine but I need to keep the header row. So I tried awk -F ',' 'NR==1; NR > 1{print > (""$2".csv")}' 2017-01.csv But I still get the same results without the header row. What am I doing wrong? I read answers to many similar questions on Stackoverflow but I just can't understand w...