IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Scripts/Batch Discussion :

Trier un fichier CSV


Sujet :

Scripts/Batch

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2018
    Messages : 4
    Par défaut Trier un fichier CSV
    J ai le fichier CSV suivant :
    "ITS-Number", "sAMAccountName-Group"
    "ITS590002", "R-LOGA"
    "ITS590002", "Smart Vision"
    "ITS590002", "R-PS3"
    "ITS590002", "P-RGP"
    "ITS591001", "R-NETKALK"
    "ITS591001", "SHILL"
    "ITS591001", "R-PS4"
    "ITS591001", "Smart Vision"
    "ITS591001", "R-LOGA"


    j'aimerais obtenir le fichier CSV ci-dessou:
    "R-LOGA","Smart Vision"


    en effet j'aimerais lister les "sAMAccountName-Group" qui reviennent plus d'une fois
    Mon Code:
    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    $InputCSV = 'C:\temp\Group.csv'
    $OutputCSV = 'C:\temp\GroupTXT'
     
    $A = import-csv $InputCSV | Group-Object sAMAccountName-Group
     
    # get the maximum number of ITS-Number values for a single sAMAccountName-Group
    $max = 0
    $A | foreach {if ($_.Count -gt $max) {$max = $_.Count}}
    $header = ""
    1..$max | ForEach { $header += ('ITS-' + $_ + ',') }  # create the header for the CSV
    $header += "sAMAccountName-Group"                   # add the last column header
    Set-Content -Path $OutputCSV -Value $header
     
    $A | ForEach {
        $empty = $max - $_.Count    # how many empty columns in this row?
        $its = @()
        ForEach ($G in $_.Group) {$its += $G.'ITS-Number'} # get each ITS-Number for this sAMAccountName-Group
        $str = $its -join ','
        $str += (',' * $empty) + ',' + $_.Name             # add the appropriate number of empty columns and the sAMAccountName-Group
        $str | Write-Output
    } | Add-Content -Path $OutputCSV

    celui ci ne fonctionne pas du tout comme prevu. Merci pour votre aide

  2. #2
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Billets dans le blog
    1
    Par défaut
    Salut,
    Citation Envoyé par Yandoy Voir le message
    celui ci ne fonctionne pas du tout comme prévu.
    Peut-être parce que tu reconstruis une structure csv, alors qu'un Export-csv sur une collection d'objet suffit le plus souvent.

    Merci pour le jeu de test

    Code Powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    @'
    "ITS-Number", "sAMAccountName-Group"
    "ITS590002", "R-LOGA"
    "ITS590002", "Smart Vision"
    "ITS590002", "R-PS3"
    "ITS590002", "P-RGP"
    "ITS591001", "R-NETKALK"
    "ITS591001", "SHILL"
    "ITS591001", "R-PS4"
    "ITS591001", "Smart Vision"
    "ITS591001", "R-LOGA"
    '@ > c:\temp\datas.csv
     
    $g=import-csv c:\temp\datas.csv| Group-Object sAMAccountName-Group
    $g|Where {$_.count -gt 1 }
     
    #Count Name                      Group
    #----- ----                      -----
    #    2 R-LOGA                    {@{ITS-Number=ITS590002; sAMAccountName-Group=R-LOGA}, @{ITS-Number=ITS591001; sAMAc...
    #    2 Smart Vision              {@{ITS-Number=ITS590002; sAMAccountName-Group=Smart Vision}, @{ITS-Number=ITS591001;...
     
     
    ($g|Where {$_.count -gt 1 }).Name
    #R-LOGA
    #Smart Vision
    ($g|Where {$_.count -gt 1 }).Name > c:\temp\Result.txt
    A voir si la dernière ligne correspond à ton besoin, car ici un fichier csv d'une colonne est à questionner :
    Code Powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    #ou un export-csv avec un objet personnalisé
    ($g|Where {$_.count -gt 1 })|
      Select-Object @{Name='sAMAccountName';Expression={$_.Name}}|
      Export-Csv -Path c:\temp\Duplicated-sAMAccountName-Group.csv

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2018
    Messages : 4
    Par défaut
    Super ! Merci Laurent

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo