Trie fichier avec SED ou AWK
Résolu
pcsystemd
Messages postés
734
Statut
Membre
-
pcsystemd Messages postés 734 Statut Membre -
pcsystemd Messages postés 734 Statut Membre -
Bonjour,
voila j'ai un fichier txt qui contient un grand nombre de lignes comme cela:
et je souhaiterais soit en utilisant AWK ou SED avoir en sortie un fichier ayant des lignes d'update avec dans le where l'id de chaque ligne comme ci-dessous :
Une idée?
Merci
--
L'accès au savoir est la première liberté que chaque homme devrait avoir.
voila j'ai un fichier txt qui contient un grand nombre de lignes comme cela:
n° id th date
220_714 | 81760 | 0 | 19/01/2010
220_2530 | 76788223 | 0 | 05/05/2006
110_2256 | 81554 | 0 | 19/01/2004
110_2563 | 101701108 | 0 | 18/10/2006
et je souhaiterais soit en utilisant AWK ou SED avoir en sortie un fichier ayant des lignes d'update avec dans le where l'id de chaque ligne comme ci-dessous :
update table set champ=0 where comp=81760; update table set champ=0 where comp=76788223; update table set champ=0 where comp=81554; update table set champ=0 where comp=101701108;
Une idée?
Merci
--
L'accès au savoir est la première liberté que chaque homme devrait avoir.
Configuration: Linux Mozilla 1.9.0.5
12 réponses
-
Tu ouvres ton fichier avec vim et tu tapes :
:%s/^[0-9_]\+[ \t]\+|[ \t]\+\([0-9]\+\) .*/update table set champ=0 where comp=\1;/g -
hello
avec awkawk 'BEGIN {FS=OFS="|"} ; /\|/ {$2=sprintf(" update table set champ=0 where comp=%d ",$2)} ; {print $0}' < fichier-
-
-
Ben ce n'est pas ce que demande notre ami il me semble ;-\
[rep]$ cat bar n° id th date 220_714 | 81760 | 0 | 19/01/2010 220_2530 | 76788223 | 0 | 05/05/2006 110_2256 | 81554 | 0 | 19/01/2004 110_2563 | 101701108 | 0 | 18/10/2006 [rep]$ awk 'BEGIN {FS=OFS="|"} ; /\|/ {$2=sprintf(" update table set champ=0 where comp=%d ",$2)} ; {print $0}' < bar n° id th date 220_714 | update table set champ=0 where comp=81760 | 0 | 19/01/2010 220_2530 | update table set champ=0 where comp=76788223 | 0 | 05/05/2006 110_2256 | update table set champ=0 where comp=81554 | 0 | 19/01/2004 110_2563 | update table set champ=0 where comp=101701108 | 0 | 18/10/2006 [rep]$ awk 'BEGIN {FS=OFS="|"} ; /\|/ {$2=sprintf(" update table set champ=0 where comp=%d ",$2)} ; {print $2}' < bar update table set champ=0 where comp=81760 update table set champ=0 where comp=76788223 update table set champ=0 where comp=81554 update table set champ=0 where comp=101701108 [rep]$;-))
-
-
-
-
Merci a tous pour les différentes façon de faire. Je vais toutes les testées et je reviens vers vous .
Merci encore -
en faites j'ai pas du tout comprendre puisque j'ai fait des essais sur d'autres fichiers mais cela ne fonctionne pas
Le contenu de mon fichier :
n° id th dft toto | 81760 | 01 02 03 04 06 | 540_256 220_2530 | tata | 02 65 45 25 26 | 654_589 110_2256 | 81554 | 02 54 68 54 26 | 202_2546 titi | tutu | 06 54 85 98 45 | 100_5648
j'ai fait un
awk 'BEGIN {FS=OFS="|"} ; /\|/ {$2=sprintf(" update table set st=1 where ref in (select uid from toto where euid='%d') and refari=140340034",$2)} ; {print $2}' <Liste.txt
mais j'ai comme résultat
update table set st=1 where ref in (select uid from toto where euid=0) and refari=140340034 update table set st=1 where ref in (select uid from toto where euid=0) and refari=140340034 update table set st=1 where ref in (select uid from toto where euid=0) and refari=140340034 update table set st=1 where ref in (select uid from toto where euid=0) and refari=140340034
Ou est l'erreur et si je peux avoir quelques explications ce serait chouette?
Merci beaucoup
L'accès au savoir est la première liberté que chaque homme devrait avoir. -
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question -
essaye avec euid=%d ou euid=\'%d\' si tu veux afficher '
-
J'avais déjà essayé ta méthode Gigaga mais cela ne fonctionnait pas j'avais comme erreur :
motif introuvable %s/^[0-9_]\+[ \t]\+|[ \t]\+\([0-9]\+\)
%s/^[0-9_]\+[ \t]\+|[ \t]\+\([0-9]\+\) /.*update table set st=1 where ref in (select uid from toto where euid=\1) and refari=140340034;/g
Merci
-
c'est ce que j'ai fait. J 'ai ouvert dans VI mon fichier puis j'ai taper les : et enfin la ligne
%s/^[0-9_]\+[ \t]\+|[ \t]\+\([0-9]\+\) /.*update table set st=1 where ref in (select uid from toto where euid=\1) and refari=140340034;/g
et j'ai l'erreur que j'indique plus haut.
Merci -
Dubcek,
j'ai essayé ce que tu m'indiques mais cela ne fonctionne pas. en effet si je saisis euid=%d jle resultat ne change pas et si je saisis euid=\'%d\' j'ai l'erreur suivante :
bash: syntax error near unexpected token `)'
Merci -
-
n° id th dft toto | 81760 | 01 02 03 04 06 | 540_256 220_2530 | tata | 02 65 45 25 26 | 654_589 110_2256 | 81554 | 02 54 68 54 26 | 202_2546 titi | tutu | 06 54 85 98 45 | 100_5648:%s/^[ \t]*\w\+[ \t]\+|[ \t]\+\(\w\+\)[ \t]\+|[ \t]\+.*[ \t]\+|.*/update table set st=1 where ref in (select uid from toto where euid=\1) and refari=140340034;/g
devient:
n° id th dft update table set st=1 where ref in (select uid from toto where euid=81760) and refari=140340034; update table set st=1 where ref in (select uid from toto where euid=tata) and refari=140340034; update table set st=1 where ref in (select uid from toto where euid=81554) and refari=140340034; update table set st=1 where ref in (select uid from toto where euid=tutu) and refari=140340034;
-
Merci beaucoup a tous et particulierement a toi Gigaga et milles excuses car je n'ai pas ete clair dans ce que je souhaite. je m'explique donc de nouveau.
je souhaite recuperer la valeur du champ dft de chaque lignes de mon fichier :
n° id th dft toto | 81760 | 01 02 03 04 06 | 540_256 gdtgh | tata | 02 65 45 25 26 | 654_589 jgkgi | 81554 | 02 54 68 54 26 | 202_2546 tuotu | tutu | 06 54 85 98 45 | 100_5648
afin d'afficher en sortie :
update table set st=1 where ref in (select uid from toto where euid=540_256) and refari=140340034; update table set st=1 where ref in (select uid from toto where euid=654_589) and refari=140340034; update table set st=1 where ref in (select uid from toto where euid=202_2546) and refari=140340034; update table set st=1 where ref in (select uid from toto where euid=100_5648) and refari=140340034;
Voila j'espere que je suis plus clair dans ma demande d'aide et encore pardon pour m'être mal expliquer
Merci -
Bon j'ai trouvé , j'ai fait diffèrement :
awk -F '|' '{print "update table set st=1 where ref in (select uid from toto where euid='\''"$4"'\'') and refari=140340053;"}' Liste.txt > sortie.txt
Merci a tous