Question [Résolu] Regex - Problème de saut de ligne

Plus d'informations
il y a 11 ans 3 mois #18529 par lucas
Bonjour,

J'essaye de faire un script pour enlever de certaine lignes des caractères, j'ai trouvé comment faire avec Regex mais après modification, dans mon fichier txt de sortie tout est à la suite.

Voila mon script:

$regex = [regex]'\d'
$a = Get-Content a.txt
$regex.Replace($a,' ','8','20') | set-content c.txt -> je veux remplacer par des espaces, 8 caractères à partir du 20eme de chaque lignes.


voici la forme de mon fichier: ( les - représente des espaces )

1111111111111111111100000000
22222222
3333333
44444444444444444444
1111111111111111111100000000
22222222
3333333
44444444444444444444
11111111111111111111
22222222
3333333
44444444444444444444
1111111111111111111100000000
22222222
3333333
44444444444444444444
11111111111111111111
22222222
3333333
44444444444444444444
11111111111111111111
22222222
3333333
44444444444444444444


je veux remplacer les \"0\" par des espaces, mais parfois je n'ai pas de \"0\" sur la ligne.
Le problème est que mon fichier de sortie C.txt ne comporte plus le même format..

un peut d'aide ?? merci d'avance<br><br>Message édité par: Arnaud, à: 8/12/14 10:38

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 3 mois #18530 par Laurent Dardenne
Salut,
essaie ceci:
[code:1]
@'
1111111111111111111100000000 22222222 3333333 44444444444444444444
1111111111111111111100000000 22222222 3333333 44444444444444444444
11111111111111111111 22222222 3333333 44444444444444444444
1111111111111111111100000000 22222222 3333333 44444444444444444444
11111111111111111111 22222222 3333333 44444444444444444444
11111111111111111111 22222222 3333333 44444444444444444444
'@ &gt; c:\temp\datas.txt

Get-Content c:\temp\datas.txt|
Foreach {
Write-host $_
#vingt chiffres en début de ligne, suivi de 8 zéros.
# On récupère le premier groupe capturé et
# on l'insére dans la chaîne de remplacement
$s=$_ -replace '^(\d{20})0{8}','$1 '
Write-host $_ -fore green
$S
}|
set-content C:\temp\c.txt
type C:\temp\c.txt
[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 3 mois #18532 par lucas
merci pour ta réponse,
oui cela fonctionne mais mon fichier de données est comme cela:

[code:1]
1258963254452400165020140109 09012014 3138390
14896524756555000579 10012014 3183194
2165651511651400290520140116 16012014 3183194
14589748522336587758 13012014 3133032
[/code:1]

aucune lignes n'est identiques (parfois + de 20 000 lignes par fichier), et les 8 caractères à remplacer correspondent à une date : 20140109 et 20140116
donc je ne peut rien mettre en \&quot;dur\&quot;.

C'est pour cela que j'était parti sur du remplacement à partir du 20ème caractère de chaque ligne.

Message édité par: lucas057, à: 4/12/14 15:46<br><br>Message édité par: lucas057, à: 4/12/14 16:00

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 3 mois #18533 par Laurent Dardenne
lucas057 écrit:

oui cela fonctionne mais mon fichier de données est comme cela:

Pourquoi spécifier un cas si c'est pour en proposer un autre une fois une réponse donnée ?<br><br>Message édité par: Laurent Dardenne, à: 4/12/14 19:10

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 3 mois #18534 par Philippe
salut lucas057

pour ta question lis cette page si tu lis le chapitre Returning a Portion of a String tu aura une solution pour modifier la script de laurent
par exemple : prendre les 20 premiers caractères rajouter 8 espaces puis prendre 29 au dernier caractère.

ci-dessous un technique pour avoir la liste des méthodes (fonctions) disponible pour des chaines de caractères
[code:1]PS C:\Users\xxx&gt; $i = \&quot;\&quot;
PS C:\Users\xxx&gt; $i | Get-Member


TypeName: System.String

Name MemberType Definition
----

Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB«»)
Contains Method bool Contains(string value)
CopyTo Method System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringComparison compar...
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, Sy...
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char valu...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOf...
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndex...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int...
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, string newV...
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int count), s...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringComparison co...
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower Method string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToString Method string ToString(), string ToString(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property System.Int32 Length {get;}
[/code:1]

une ou des méthodes suivantes pourrais te servir :
Substring ou Length

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 3 mois #18535 par lucas
Excuse moi mais je ne voulais pas afficher sur la toile mon fichier \&quot;datas\&quot; qui correspond à des infos de facturation..

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.051 secondes
Propulsé par Kunena