instinctless
Goto Top

Regex Foo Textdatei manulieren

Hallo Leute,
ich habe eine Text/csv Datei welche ich manipulieren möchte.

Ausgangssitutuation:
AK026_P2_t6_R2,,
AK027_P2_t6_W1,,
AK028_P2_t6_W2,,
AK029_P2_t6_S1,,
AK030_P2_t6_S2,,

soll zu
Sample_AK026_P2_t6_R2,AK026_P2_t6_R2,
Sample_AK027_P2_t6_W1,AK027_P2_t6_W1,
Sample_AK028_P2_t6_W2,AK028_P2_t6_W2,
Sample_AK029_P2_t6_S1,AK029_P2_t6_S1,
Sample_AK030_P2_t6_S2,AK030_P2_t6_S2,
geändert werden.

Wie stelle ich das an?

Content-Key: 3409984757

Url: https://administrator.de/contentid/3409984757

Printed on: April 20, 2024 at 00:04 o'clock

Member: Gentooist
Gentooist Jul 22, 2022 at 08:41:38 (UTC)
Goto Top
Z.B. mit Pandas von Python; Dataframe manipulieren.
Member: colinardo
Solution colinardo Jul 22, 2022 updated at 09:05:48 (UTC)
Goto Top
Servus.
mit Linux auf der Shell mit (g)awk
awk -F "," '{print "Sample_"$1","$1","}' test.csv >test_neu.csv  
oder auch mit sed
 sed -re 's/(^[^,]+).*/Sample_\1,\1,/' test.csv >test_neu.csv  
Powershell z.B.
$file = 'D:\test.csv'
(Get-Content $file) -replace '([^,]+).*','Sample_$1,$1,' | Set-Content $file
oder für die "objektorientierten" Regex unerfahrenen Nutzer
$file = 'D:\test.csv'  
(Import-CSV $file -Delimiter "," -Header (1..3) | %{  
    $_.2 = $_.1
    $_.1 = "Sample_$($_.1)"  
    $_
} | convertto-CSV -Delimiter "," -NoType | select -Skip 1) -replace '"' | Set-Content $file  
usw. dafür gibt es 1001 Möglichkeiten ...

Grüße Uwe
Member: instinctless
instinctless Jul 22, 2022 at 10:51:41 (UTC)
Goto Top
Vielen Dank Uwe