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 :

script batch travaillant sur du *.txt


Sujet :

Scripts/Batch

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 9
    Par défaut script batch travaillant sur du *.txt
    Bonjour,

    je cherche à écrire un script batch qui prend un fichier text en entrée, et remplace dans celle ci toute les tabulations par un point virgule, et supprime certain mot clé.
    je trouve pas les bonnes commandes permettant ça.

    Merci de m'aider.

    @+

  2. #2
    Expert confirmé
    Avatar de shawn12
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Avril 2006
    Messages
    3 368
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet en SSII
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2006
    Messages : 3 368

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 9
    Par défaut
    Citation Envoyé par shawn12 Voir le message
    Merci à vous,
    mais cette commande n'est pas reconnu en tant que commande interne ou externe, je travaille sur windows XP

  4. #4
    Membre expérimenté
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    240
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juillet 2008
    Messages : 240
    Par défaut
    Bonjour
    il faut placer ta commande avec ton fichier bat et txt.
    ou bien indiquer le chemin de la commande dans ton bat.

  5. #5
    Membre chevronné

    Homme Profil pro
    Analyste d'exploitation
    Inscrit en
    Décembre 2003
    Messages
    333
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Analyste d'exploitation
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 333
    Billets dans le blog
    1
    Par défaut
    Tu peux utiliser un parsage ligne par ligne de ton fichier avec les fonctions de remplacement natives du shell windows :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @echo off& setlocal ENABLEDELAYEDEXPANSION& cd /d "%~dp0"& cls
    rem set vars
    set MyOccurenceToReplace=Mod
    set MyfiletoProceed=Mon_fichier.txt
    
    del /q /f tmp_file.txt >nul 2>&1
    for /f "delims=" %%i in (!MyfiletoProceed!) do (
        set StringToProceed=%%i
        (echo !StringToProceed:%MyOccurenceToReplace%=!)>>tmp_file.txt
    )
    del /q /f !MyfiletoProceed! >nul
    type tmp_file.txt > !MyfiletoProceed!
    del /q /f tmp_file.txt >nul
    echo End of script & pause & exit
    Comme utilitaire de remplacement (issue de unix, libre), tu as aussi sed, gsar et sfk que tu peux utiliser.

    Ex, help file de Gsar :

    Code : 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
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
        NAME
                     gsar - General Search And Replace utility
    
    
              SYNOPSIS
                     gsar [options] [infile(s)] [outfile]
    
    
              DESCRIPTION
                     gsar (General Search And Replace) is a utility for search-
                     ing for and --- optionally --- replacing strings  in  both
                     text  and binary files. The search and replace strings can
                     contain all kinds of characters (0--255), i.e. Ctrl  char-
                     acters and extended ASCII as well.
    
                     The  algorithm  used  is  a  variation  of the Boyer-Moore
                     search algorithm, modified to search binary  files.  As  a
                     result of this, gsar is blindingly fast.
    
                     Opposed  to  line oriented search programs (like grep(1)),
                     gsar will find all  matches  on  a  line.  Actually,  gsar
                     doesn't  know  anything  about lines at all, all files and
                     strings are treated as binary.
    
                     Gsar can search one or several  files  for  a  string  and
                     report the occurrences. Gsar can read one file, search for
                     a string, replace it with some other string, and create  a
                     new file containing the changes. Gsar can perform a search
                     and replace in multiple files, overwriting the  originals.
                     Finally,  gsar can work as a filter, reading from standard
                     input and writing to standard output.
    
    
              OPTIONS
                     All options can be concatenated into one single option i.e
                     the command: gsar  -i -b -l is the same as gsar -ibl
    
                     An  option which takes an argument must be the last one in
                     the concatenated option, since the rest of the  option  is
                     taken as a possible argument.
    
                     Fields  enclosed  in  []  are optional, but mandatory when
                     enclosed in .  Options are case sensitive i.e -b is  not
                     the same as -B.
    
                     If no options are given, gsar just gives a brief help mes-
                     sage.
    
                 [infile(s)]
                     Name(s) of input file(s) (wildcards allowed on  most  Unix
                     shells  and  most DOS compilers). If the -F option is used
                     input is taken from stdin.
    
                 [outfile]
                     Name of output file that is to contain  the  replacements.
                     If  the  -F  option is used, transformed output is sent to
                     stdout.
    
                 -s
                     String to search for  in  file.  Ctrl  characters  can  be
                     entered by using a `:' in the string followed by the ASCII
                     value of the character. The value is entered using  a  `:'
                     followed  by  three decimal digits or `:x' followed by two
                     hex numbers.  To enter a colon (:) in the  search  pattern
                     use `::'.  The string must follow directly after s.
    
                     Example:  To  search for the string :foo (`o' is 111 deci-
                     mal, 6F in hex) use the search options:
                       -s::foo or -s::fo:111 or -s::fo:x6F
    
                     If you want to search for a  string  with  spaces  in  it,
                     under  MSDOS  surround  the expression with quotes.  Under
                     Unix, use the mechanisms  your  shell  provides  (commonly
                     quotes) to include space or other special characters.
    
                     Example: search for gsar is fast use:
                       gsar "-sgsar is fast" foobar.txt
    
                     The  precompiled  MSDOS executable in the archive supports
                     response files. Just put you gsar commands into a file and
                     put  a  `@'  in  front of the filename on the gsar command
                     line.
    
                     Example: file foobar.txt contains  -ssupercalifragilistic-
                     expialidocus:
                       gsar @foobar.txt poppins.txt
    
                     If response files are needed, most Unix shells will allow
                       gsar `cat foobar.txt` poppins.txt
    
                 -r\[string]
                     String  which  is to replace search string in file. Use -r
                     to delete the search string from  the  file  i.e.  replace
                     with  nothing.  Ctrl characters can be entered in the same
                     way as in the search string. If this option is  left  out,
                     gsar  only  performs  a  search.  The  string  must follow
                     directly after r.
    
                 -i
                     Ignore case difference when comparing strings.  I.e.  foo-
                     bar matches fooBAR.
    
                 -B
                     Just  display  the search & replace buffers, for test pur-
                     poses.
    
                 -f
                     If the output file already exists this switch can be  used
                     to force an overwrite of the existing output file.
    
                 -o
                     Search  and replace of multiple files, overwrite the input
                     file(s). For each input  file,  gsar  creates  a  tempfile
                     which contains the replacements and copies the tempfile to
                     the original input file name. If no  matches  were  found,
                     the input file stays the same. The tempfile is removed.
    
                     Example:
                       gsar -s__ZTC__ -r__TURBOC__ -o foo.c bar.c bat.c
                     The files foo.c, bar.c & bat.c are all changed.
    
                 -c\[n]
                     Display  the  context  around a match in a textual manner.
                     Undisplayable characters are displayed as a dot  (`.').  n
                     is  optional  number  of  bytes  in context. n must follow
                     directly after c.
    
                 -x\[n]
                     Display the context around a match as a hexadecimal  dump.
                     Undisplayable  characters  are displayed as a dot (`.'). n
                     is optional number of bytes  in  context.  n  must  follow
                     directly after x.
    
                 -b
                     Display the byte offset of the match in hex.
    
                 -l
                     Only list filename and number of matches if any (default).
    
                 -h
                     Suppress display of filename when  displaying  context  or
                     byte offsets.
    
                 -du
                     Convert a DOS ASCII file to UNIX (strips carriage return).
    
                 -ud
                     Convert a UNIX ASCII file to DOS (adds carriage return).
    
                 -F
                     Filter mode, gsar takes it's input from  stdin  and  redi-
                     rects  eventual  output  to stdout. All error messages are
                     sent stderr.
    
                 -G
                     Display the GNU General Public Licence.
    
    
              Examples
                     Search for two spaces at the end of a line (DOS text)  and
                     replace with just a carriage return overwriting the origi-
                     nal files:
    
                       gsar  -s:x20:x20:x0d  -r:x0d  -o  foobar.txt  *.c
    
                     Convert a UNIX text file to  DOS  format  overwriting  the
                     original file:
    
                       gsar  -ud  -o  unix.txt
    
                     Search  for  the  string  WATCOM  and replace with __ZTC__
                     using gsar as a filter. Output  is  redirected  to  a  new
                     file:
    
                       gsar  -sWATCOM  -r__ZTC__  -F    foo_z.c
    
                     Display  textual  context  of the string error in the file
                     gsar.exe disregarding case. With 40 bytes in the context:
    
                       gsar  -serror  -i  -c40  gsar.exe
    
                     Search for the string gnu in the file fsf and  replace  it
                     with wildebeest, creating a new output file africa:
    
                       gsar  -sgnu  -rwildebeest  fsf  africa
    
                     (if  the file africa exists, you have to use the -f option
                     to overwrite it.)
    
                     Search for the string error in the  file  command.com  and
                     display the byte offset of each match:
    
                       gsar  -serror  -b  command.com
    
    
              Limitations
                     No  wildcards  or  regular  expressions  allowed in search
                     string.
    
                     On MSDOS platforms stdin from a tty is not allowed because
                     stdin  has  been  turned into binary. MSDOS will not catch
                     the Ctrl-Z signifying EOF.
    
              Authors
                     Tormod Tjaberg (coding, design (all bugs are his))
                     Hans Peter Verne (ideas, demands, testing, UNIX platforms)
    
                     If you have any comments, bug reports or whatever, we can
                     be reached through email at:
    
                     tjaberg@online.no
                     hpv@kjemi.uio.no

Discussions similaires

  1. Réponses: 1
    Dernier message: 27/11/2008, 11h41
  2. Réponses: 0
    Dernier message: 15/09/2008, 10h03
  3. Batch : Traitement sur fichier txt
    Par vita83 dans le forum Scripts/Batch
    Réponses: 2
    Dernier message: 13/09/2008, 08h08
  4. Travail sur chaine de caractère d'un fichier txt
    Par ginkas31 dans le forum Requêtes et SQL.
    Réponses: 3
    Dernier message: 21/09/2007, 15h35
  5. Exécuter un script batch sur un pc distant
    Par koKoTis dans le forum Scripts/Batch
    Réponses: 21
    Dernier message: 22/07/2007, 19h05

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