Last active
August 29, 2015 14:01
-
-
Save dmalikov/270ebb984e882284b958 to your computer and use it in GitHub Desktop.
Groupping strings by multiple fileds in various languages (All Hail Powershell)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!awk -f | |
BEGIN { | |
FS = "." | |
} | |
{ | |
if (! (map[$2][$5])) { | |
print map[$2][$5] = $0 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Eu where | |
import Control.Applicative ((<$>)) | |
import Data.Function (on) | |
import Data.List (groupBy, intercalate) | |
import Data.List.Split (splitOn) | |
import qualified Data.Map as M | |
import Data.Maybe (mapMaybe) | |
main :: IO () | |
main = do | |
records <- lines <$> readFile "file" | |
mapM_ putStrLn $ | |
concatMap (\(a,b) -> map (\c -> fromTuple (a,c)) b) $ | |
map (\xs -> (fst . head $ xs, M.toList . M.fromListWith (const id) $ map snd xs)) $ | |
groupBy ((==) `on` fst) $ | |
mapMaybe toTuple records | |
-- | | |
-- >>> toTuple "SFE.BasicFlow.Evg.USWE1.USCE1" | |
-- Just ("BasicFlow",("USCE1","USWE1")) | |
-- >>> toTuple "SFE.BasicFlow.Evg.USWE1.USCE1.Nope" | |
-- Nothing | |
toTuple :: String -> Maybe (String, (String, String)) | |
toTuple s = case splitOn "." s of | |
[_, a, _, b, c] -> Just (a, (c, b)) | |
_ -> Nothing | |
-- | | |
-- >>> fromTuple ("BasicFlow",("USCE1","USWE1")) | |
-- "SFE.BasicFlow.Evg.USWE1.USCE1" | |
fromTuple :: (String, (String, String)) -> String | |
fromTuple (a,(b,c)) = intercalate "." ["SFE", a, "Evg", c, b] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gc -pa "file" | group -pr { $_.Split(".")[1,4] } | %{ $_.group[0] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> INPUT | |
SFE.BasicFlow.Evg.USWE1.USCE1 | |
SFE.BasicFlow.Evg.USWE1.EUNO1 | |
SFE.BasicFlow.Evg.USWE1.USEA2 | |
SFE.BasicFlow.Evg.EUNO1.USCE1 | |
SFE.BasicFlow.Evg.EUNO1.EUNO1 | |
SFE.BasicFlow.Evg.EUNO1.USEA2 | |
SFE.BasicFlow.Evg.USEA1.USCE1 | |
SFE.BasicFlow.Evg.USEA1.EUNO1 | |
SFE.BasicFlow.Evg.USEA1.USEA2 | |
SFE.BasicFlow.Evg.USEA1.EUWE1 | |
SFE.BasicFlow.Evg.USNC1.USCE1 | |
SFE.BasicFlow.Evg.USNC1.EUNO1 | |
SFE.BasicFlow.Evg.USNC1.USEA2 | |
SFE.BasicFlow.Evg.USNC1.EUWE1 | |
SFE.BasicFlow.Evg.EUWE1.USCE1 | |
SFE.BasicFlow.Evg.EUWE1.EUNO1 | |
SFE.BasicFlow.Evg.EUWE1.USEA2 | |
SFE.BasicFlow.Evg.EUWE1.EUWE1 | |
SFE.DcRedirectFlow.Evg.USWE1.USEA2 | |
SFE.DcRedirectFlow.Evg.USWE1.EUWE1 | |
SFE.DcRedirectFlow.Evg.EUNO1.EUNO1 | |
SFE.DcRedirectFlow.Evg.EUNO1.USEA2 | |
SFE.DcRedirectFlow.Evg.EUNO1.EUWE1 | |
SFE.DcRedirectFlow.Evg.USEA1.EUNO1 | |
SFE.DcRedirectFlow.Evg.USEA1.USEA2 | |
SFE.DcRedirectFlow.Evg.USEA1.EUWE1 | |
SFE.DcRedirectFlow.Evg.USNC1.EUNO1 | |
SFE.DcRedirectFlow.Evg.USNC1.USEA2 | |
SFE.DcRedirectFlow.Evg.USNC1.EUWE1 | |
SFE.DcRedirectFlow.Evg.EUWE1.EUNO1 | |
SFE.DcRedirectFlow.Evg.EUWE1.USEA2 | |
SFE.DcRedirectFlow.Evg.EUWE1.EUWE1 | |
SFE.BingChatFlow.Evg.USWE1.USCE1 | |
SFE.BingChatFlow.Evg.USWE1.EUNO1 | |
SFE.BingChatFlow.Evg.USWE1.EUWE1 | |
SFE.BingChatFlow.Evg.EUNO1.USCE1 | |
SFE.BingChatFlow.Evg.EUNO1.EUNO1 | |
SFE.BingChatFlow.Evg.EUNO1.EUWE1 | |
SFE.BingChatFlow.Evg.USEA1.USCE1 | |
SFE.BingChatFlow.Evg.USEA1.EUNO1 | |
SFE.BingChatFlow.Evg.USEA1.EUWE1 | |
SFE.BingChatFlow.Evg.USNC1.USCE1 | |
SFE.BingChatFlow.Evg.USNC1.EUNO1 | |
SFE.BingChatFlow.Evg.USNC1.EUWE1 | |
SFE.BingChatFlow.Evg.EUWE1.USCE1 | |
SFE.BingChatFlow.Evg.EUWE1.EUNO1 | |
SFE.BingChatFlow.Evg.EUWE1.USEA2 | |
SFE.BingChatFlow.Evg.EUWE1.EUWE1 | |
<<< OUTPUT | |
SFE.BasicFlow.Evg.USWE1.USCE1 | |
SFE.BasicFlow.Evg.USWE1.EUNO1 | |
SFE.BasicFlow.Evg.USWE1.USEA2 | |
SFE.BasicFlow.Evg.USEA1.EUWE1 | |
SFE.DcRedirectFlow.Evg.USWE1.USEA2 | |
SFE.DcRedirectFlow.Evg.USWE1.EUWE1 | |
SFE.DcRedirectFlow.Evg.EUNO1.EUNO1 | |
SFE.BingChatFlow.Evg.USWE1.USCE1 | |
SFE.BingChatFlow.Evg.USWE1.EUNO1 | |
SFE.BingChatFlow.Evg.USWE1.EUWE1 | |
SFE.BingChatFlow.Evg.EUWE1.USEA2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment