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 :

récupérer une ligne avec findstr


Sujet :

Scripts/Batch

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 7
    Points : 5
    Points
    5
    Par défaut récupérer une ligne avec findstr
    bonjour a toutes et tous :

    dans un batch , je récupère toutes les lignes d'un fichier texte qui contiennent "|FR|" jusqu'ici tout fonctionne correctement !
    mais j'aimerais également récupérer la ligne qui se trouve juste après celle ou le "|FR|" à été trouvé ...

    je n'ai pas réussi pour le moment

    un peu d'aide serai la bienvenue car la , je patauge un peu !!

    voici le code qui récupère les lignes contenant le "|FR|" et qui fonctionne !
    MERCI DE VOTRE AIDE

    Franck

    Code Batch : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    @echo off 
     
    for /f "delims=" %%a in ('dir /b/s yacine.txt') do (
    findstr "|FR|"  "%%a" >> resultat.txt
    )

  2. #2
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut

    Pouvez-vous nous ajouter le fichier input et le résultat attendu après le traitement !

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    Citation Envoyé par hackoofr Voir le message

    Pouvez-vous nous ajouter le fichier input et le résultat attendu après le traitement !
    bonjour

    voici un extrait de l'entrée (juste quelques lignes car le fichier est énorme)

    #EXTINF:-1,|EN| Serial Experiments Lain S01 E11
    http://line.hi-ott.net:80/series/001
    #EXTINF:-1,|EN| Serial Experiments Lain S01 E12
    http://line.hi-ott.net:80/series/002
    #EXTINF:-1,|EN| Serial Experiments Lain S01 E13
    http://line.hi-ott.net:80/series/0089
    #EXTINF:-1,|FR| Born to Kill S01 E01
    http://line.hi-ott.net:80/series/01236
    #EXTINF:-1,|FR| Born to Kill S01 E02
    http://line.hi-ott.net:80/series/4586
    #EXTINF:-1,|FR| Born to Kill S01 E03
    http://line.hi-ott.net:80/series/47896

    et donc je voudrais en sortie les lignes qui contiennent "|FR|" + la ligne juste en dessous soit :

    #EXTINF:-1,|FR| Born to Kill S01 E01
    http://line.hi-ott.net:80/series/01236
    #EXTINF:-1,|FR| Born to Kill S01 E02
    http://line.hi-ott.net:80/series/4586
    #EXTINF:-1,|FR| Born to Kill S01 E03
    http://line.hi-ott.net:80/series/47896

    merci pour votre aide

  4. #4
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut

    Je vous propose une solution en Powershell utilisant l'expression régulière RegEx
    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
    cls
    $Data = @("
    #EXTINF:-1,|EN| Serial Experiments Lain S01 E11
    http://line.hi-ott.net:80/series/001
    #EXTINF:-1,|EN| Serial Experiments Lain S01 E12
    http://line.hi-ott.net:80/series/002
    #EXTINF:-1,|EN| Serial Experiments Lain S01 E13
    http://line.hi-ott.net:80/series/0089
    #EXTINF:-1,|FR| Born to Kill S01 E01
    http://line.hi-ott.net:80/series/01236
    #EXTINF:-1,|FR| Born to Kill S01 E02
    http://line.hi-ott.net:80/series/4586
    #EXTINF:-1,|FR| Born to Kill S01 E03
    http://line.hi-ott.net:80/series/47896
    ")
     
    $pattern = '(#EXTINF:-1,\|FR\|).*[\s\S](http://.*)'
    $results = $data | Select-String $Pattern -AllMatches
    $results.Matches.Value

  5. #5
    Membre éprouvé
    Femme Profil pro
    ..
    Inscrit en
    Décembre 2019
    Messages
    562
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 94
    Localisation : Autre

    Informations professionnelles :
    Activité : ..

    Informations forums :
    Inscription : Décembre 2019
    Messages : 562
    Points : 1 253
    Points
    1 253
    Par défaut
    Salut,

    Plus peut-être une pour cmd, ça fait deux

    Code batch : 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
    28
    @echo off
    setlocal
    set "src=fichier.txt"
    set "lang_list=fr en"
    ::==============================================
    for /f %%i in ('dir /s /b "%~dp0%src%"') do (
      echo  src: %%~i
      for %%L in (%lang_list%) do (
      call :extract %%~L "%%~i" > "%%~dpni.%%~L%%~xi"
      echo    %%L: %%~dpni.%%~L%%~xi
      start "" notepad "%%~dpni.%%~L%%~xi"
    ))
    endlocal
    goto :eof
     
    :extract
    for /f "usebackq tokens=*" %%i in ("%~2") do (
    if defined print_next (
      echo %%i
      set "print_next="
    ) else (
      echo "%%~i" | findstr /i /c:"|%~1|" > nul
      if not errorlevel 1 (
        echo %%i
        set "print_next=ok"
      )
    ))
    goto :eof

  6. #6
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut

    Une autre façon avec un batch utilisant les expressions régulières avec un vbscript
    Vous pouvez glisser et déposer (Drag and Drop) votre fichier m3u afin d'extraire les données souhaitées
    Code BAT : 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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    @echo off & color 0A & Mode 78,5
    Title Batch File To Extract Data From m3u file using regex with vbscript by Hackoo 2020
    Set String2Find="|FR|"
    Set "InputFile=%~1"
    If ["%InputFile%"] EQU [""] Goto Help
    cls
    echo(
    ECHO      ******************************************************************
    echo                   Extracting M3U Links is in Progress...
    ECHO      ******************************************************************
    Set "TmpFile=%~dp0Tmp.txt"
    @for %%a in ("%InputFile%") do (
        set "FileName=%%~na"
        set "Ext=%%~xa"
    )
    Call :Remove_special_chars %String2Find% NewString
    Set "OutPutFile=%NewString%_%FileName%%Ext%"
    If Exist "%~dp0%OutPutFile%" Del "%~dp0%OutPutFile%"
    echo #EXTM3U>"%~dp0%OutPutFile%"
    Call :Extract "%InputFile%" "%TmpFile%" %String2Find%
    Call :RemoveEmptyLines "%TmpFile%" "%OutPutFile%"
    Explorer /e,/select,"%~dp0%OutPutFile%"
    Del "%TmpFile%" & Exit
    REM ----------------------------------------------------------------------------------
    :Remove_special_chars <OLD_String> <New_String>
    @for /f "delims=" %%a in ('Powershell -C '"%~1"' -replace '[\W]',""') do set "%2=%%a"
    Exit /B
    REM ----------------------------------------------------------------------------------
    :Extract <InputData> <OutPutData> <String2Find>
    (
    echo Data = WScript.StdIn.ReadAll
    echo Pattern = Replace("(#EXTINF:-1,%~3).*[\s\S](http://.*)" , "|" ,"\|"^)
    echo Data = Extract(Data,Pattern^)
    echo WScript.StdOut.WriteLine Data
    echo Function Extract(Data,Pattern^)
    echo    Dim oRE,oMatches,Match,Line
    echo    set oRE = New RegExp
    echo    oRE.IgnoreCase = True
    echo    oRE.Global = True
    echo    oRE.Pattern = Pattern
    echo    set oMatches = oRE.Execute(Data^)
    echo    If not isEmpty(oMatches^) then
    echo        For Each Match in oMatches  
    echo            Line = Line ^& Match.Value ^& vbcrlf
    echo        Next
    echo        Extract = Line 
    echo    End if
    echo End Function
    )>"%tmp%\%~n0.vbs"
    cscript /nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
    If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
    exit /b
    REM ----------------------------------------------------------------------------------
    :RemoveEmptyLines <InputFile> <OutPutFile>
    Powershell.exe -C "(GC '%1').Trim() | ? {$_.Length -gt 0} | Add-Content '%2'"
    Exit /b
    REM ----------------------------------------------------------------------------------
    :Help
    Color 0C
    echo(
    echo             You should drag and drop a file over, 
    echo             this script "%~nx0" to extract data !
    Timeout /T 10 /NoBreak>nul
    Exit
    REM ----------------------------------------------------------------------------------

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    Citation Envoyé par hackoofr Voir le message
    merci beaucoup pour cette solution je ne connais pas trop PowerShell ma je vais regarder cela de plus près

    Citation Envoyé par kaitlyn Voir le message

    ok super merci pour la réponse je vais tester cela

    Citation Envoyé par hackoofr Voir le message
    Un grand merci pour vos réponses ca m'aide beaucoup.
    je vais pouvoir progresser un peu grâce a vous ... c'est top !

  8. #8
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut Extract-M3U_by_Word.vbs

    Puisque ce script me convient très bien dans mes transformations des fichiers de type m3u, alors j'ai développé ce vbscript !
    Juste il faut glisser et déposer le fichier .m3u sur le vbscript et le tour est joué
    Code VB : 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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    Option Explicit
    Dim ws,fso,Title,InPutFile,Pattern,OutPutFile,Data,MyString2Find,scriptdir,ALLMyStrings,MyString,M3U_Files
    Title = "Extracting Data from M3U file by Word "& chrW(169) &" Hackoo 2020"
    If wscript.arguments.count > 0 Then
    ' The InPutFile is the file to be dragged and dropped over this Vbscript
        InPutFile = Wscript.Arguments(0)
        Data = ReadFile(InPutFile)
        MyString2Find = InPutBox(ExtractFileName(InPutFile) & vbCrLF & "Tapez un ou plusieurs mot(s) à rechercher séparés par un point virgule "";"" : " &_
        vbcrlf & vbcrlf &"Exemple : "& vbCrLf &_
        "BEIN;RMC;OSN;MYHD;FR;AR;EN;SP;CANAL",Title,"BEIN;RMC;OSN;MYHD;|FR|;FR;AR;|AR|;EN;SP;CANAL;ORANGE;OCS")
        IF MyString2Find = "" Then Wscript.Quit(1)
        ALLMyStrings = Split(MyString2Find,";")
        Set fso = CreateObject("Scripting.FileSystemObject")
        'scriptdir = fso.GetParentFolderName(WScript.ScriptFullName)
        M3U_Files = GetFilenameWithoutExtension(InPutFile) &"_M3U_Files"
        Call SmartCreateFolder(M3U_Files)
        For Each MyString In ALLMyStrings 
            Pattern = Replace("(#EXTINF:-1,"& MyString &").*[\s\S](http://.*)" , "|" ,"\|")
            MyString = Replace(MyString ,"|","_")
            OutPutFile = MyString & "_" & ExtractFileName(InPutFile)
            Write2File "#EXTM3U",M3U_Files &"\"& OutPutFile,2
            Write2File Extract(Data,Pattern),M3U_Files &"\"& OutPutFile,8
        Next
        Explorer M3U_Files
    Else
        Call Display_Help_Usage()
    End If
    '-----------------------------------------------------------------
    Function Extract(Data,Pattern)
       Dim oRE,oMatches,Match,Line
       set oRE = New RegExp
       oRE.IgnoreCase = True
       oRE.Global = True
       oRE.Pattern = Pattern
       set oMatches = oRE.Execute(Data)
       If not isEmpty(oMatches) then
           For Each Match in oMatches  
               Line = Line & Match.Value & vbLf
           Next
           Extract = Line 
       End if
    End Function
    '------------------------------------------------
    Function GetFilenameWithoutExtension(FileName)
        Dim Result, i
        Result = FileName
        i = InStrRev(FileName, ".")
        If ( i > 0 ) Then
            Result = Mid(FileName, 1, i - 1)
        End If
        GetFilenameWithoutExtension = Result
    End Function
    '------------------------------------------------
    Sub Display_Help_Usage()
        Dim ws
        Set ws = CreateObject("wscript.shell")
        ws.Popup "You should drag and drop a M3U file over this program " & vbCrLF &_
        chr(34) & Wscript.ScriptName & chr(34) & vbCrLF &_
        " to extract data !",8,Title,vbExclamation
        Wscript.Quit(1)
    End Sub
    '-----------------------------------------------------------------
    Sub Explorer(Object)
        Dim ws
        Set ws = CreateObject("wscript.shell") 
        ws.run "Explorer /n,/select,"& Object &"",1,True
    End Sub
    '-----------------------------------------------------------------
    Function ReadFile(InPutFile)
        Dim objFSO,oTS,sText
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set oTS = objFSO.OpenTextFile(InPutFile)
        sText = oTS.ReadAll
        oTS.close
        set oTS = nothing
        Set objFSO = nothing
        ReadFile = sText
    End Function 
    '-----------------------------------------------------------------
    Sub Write2File(strText,OutPutFile,Mode)
        Dim fs,ts 
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(OutPutFile,Mode,True)
        ts.WriteLine strText
        ts.Close
    End Sub
    '-----------------------------------------------------------------
    Function ExtractFileName(strMyPath)
        ExtractFileName = Mid(strMyPath, InStrRev(strMyPath, "\") + 1)
    End Function
    '-----------------------------------------------------------------
    Sub SmartCreateFolder(strFolder)
        With CreateObject("Scripting.FileSystemObject")
            If Not .FolderExists(strFolder) then
                SmartCreateFolder(.getparentfoldername(strFolder))
                .CreateFolder(strFolder)
            End If
        End With 
    End Sub
    '-----------------------------------------------------------------

Discussions similaires

  1. Réponses: 3
    Dernier message: 05/09/2008, 10h32
  2. [HTML] comment récupérer une ligne d'un tableau avec HTML?
    Par jaafarerraji dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 01/10/2007, 00h21
  3. Récupérer une ligne aléatoire avec fgets
    Par Alexbad dans le forum C
    Réponses: 20
    Dernier message: 30/07/2006, 23h53
  4. [FP]Tracer Une ligne avec Dev-pascal
    Par yffick dans le forum Turbo Pascal
    Réponses: 9
    Dernier message: 17/12/2003, 16h33
  5. supprimer une ligne avec cle etrangere
    Par BaBas dans le forum Langage SQL
    Réponses: 4
    Dernier message: 15/07/2003, 11h24

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