Re: Bash script - pass command line arg to embedded sed script with multiple args
On Sat, Apr 17, 2010 at 12:02:40PM -0400, Daniel D Jones wrote: On Saturday 17 April 2010 00:09:28 Michael Elkins wrote: On Fri, Apr 16, 2010 at 08:15:38PM -0400, Daniel D Jones wrote: What I'm trying to do is pretty simple. Getting it to work is turning out not to be. What I want to do is call a bash script with a couple of arguments, and, within the script, call sed to use those args to replace two placeholders in a file: bashscript SUB1 SUB2 This line inside bashscript doesn't work: sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' input output If you switch the single quotes to double quotes it will work as you expect. Variables inside of double quotes are expanded. Single quotes are for literal strings, as you've discovered. That was the first thing I tried and sed gave me an error: sed: -e expression #1, char 18: unknown option to `s' I just went back and tried it again and it worked, so I have no idea what I did the first time that made it not work. As others have mentioned, rgw command-line parameter $1 probably has a '/' in it. A simple workaround is to use a different character as the separator. That is: *if* you can assume that variable will not contain the character '|', you can use: sed -e s|PLACEHOLDER1|$1|' -e s|PLACEHOLDER2|$2| input output You can use some other characters there as well. See sed(1). -- Tzafrir Cohen | tzaf...@jabber.org | VIM is http://tzafrir.org.il || a Mutt's tzaf...@cohens.org.il || best tzaf...@debian.org|| friend -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20100420041023.go16...@pear.tzafrir.org.il
Re: Bash script - pass command line arg to embedded sed script with multiple args
On Saturday 17 April 2010 00:09:28 Michael Elkins wrote: On Fri, Apr 16, 2010 at 08:15:38PM -0400, Daniel D Jones wrote: What I'm trying to do is pretty simple. Getting it to work is turning out not to be. What I want to do is call a bash script with a couple of arguments, and, within the script, call sed to use those args to replace two placeholders in a file: bashscript SUB1 SUB2 This line inside bashscript doesn't work: sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' input output If you switch the single quotes to double quotes it will work as you expect. Variables inside of double quotes are expanded. Single quotes are for literal strings, as you've discovered. That was the first thing I tried and sed gave me an error: sed: -e expression #1, char 18: unknown option to `s' I just went back and tried it again and it worked, so I have no idea what I did the first time that made it not work. -- Clear writers assume, with a pessimism born of experience, that whatever isn't plainly stated the reader will invariably misconstrue. - John R. Trimble -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/201004171202.40262.ddjo...@riddlemaster.org
Re: Bash script - pass command line arg to embedded sed script with multiple args
On Sat, Apr 17, 2010 at 12:02:40PM -0400, Daniel D Jones wrote: That was the first thing I tried and sed gave me an error: sed: -e expression #1, char 18: unknown option to `s' I just went back and tried it again and it worked, so I have no idea what I did the first time that made it not work. You can run into that sort of problem if your pattern to replace contains any forward slashes (/) in it. If you need to such an expansion, you probably want to do it in two passes, first doing a / to \/ substitution on your replacement strings, then inserting them into your final expression: pata=`echo $1 | sed 's,/,\/,'` patb=`echo $2 | sed 's,/,\/,'` sed -e s/PATTERN1/$pata/ -e s/PATTERN2/$patb/ input output me pgpXRZbLnPuiG.pgp Description: PGP signature
Re: Bash script - pass command line arg to embedded sed script with multiple args
Daniel D Jones ddjo...@riddlemaster.org wrote: What I want to do is call a bash script with a couple of arguments, and, within the script, call sed to use those args to replace two placeholders in a file: bashscript SUB1 SUB2 This line inside bashscript doesn't work: sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' input output Single quotes tells the shell to use the contents verbatim. Double quotes allows the shell to interpolate variables. Note that sed does not get to see the quotes at all (see below). It doesn't work because the command line args ($1, $2) are quoted and don't get replaced. Yes... However, because I'm using the -e argument to sed, I have to quote the argument or sed gets all huffy. This is a red herring. The -e argument allows you to introduce multiple operations. Omit it and you get just a single shot (as you demonstrate). Sed doesn't see the quotation marks at all, so you're slightly on the wrong track with your reasoning. The only workaround I've found is to do the substitutions in two passes: sed s/SUB1/$1/ input temp sed s/SUB2/$2/ temp output Which is almost exactly the same as this: sed s/SUB1/$1/ input temp sed s/SUB2/$2/ temp output *except* that in my case the $1 and $2 can contain whitespace, but in yours they can't. (In neither case can they contain /.) Since I'm not using multiple argument to sed, I don't have to quote the arg. As explained above, this is incorrect. I suppose I could pipe one sed command to another rather than using a temp file but that's not significantly more palatable. Given the knowledge constraints under which you're working, I'm curious to understand why you prefer a temporary file to a pipe. Any ideas on a cleaner approach on how to do this, either by getting this right or using an alternative method, are welcome [...] sed -e s/PLACEHOLDER1/$1/ -e s/PLACEHOLDER2/$2/ input output perl -pe 'BEGIN {($a,$b)=splice(@ARGV,0,2)} s/PLACEHOLDER1/$a/;s/PLACEHOLDER2/$b/' $1 $2 input output The potential advantage of the perl version is that the variable can contain not only whitespace but also / characters. I don't know if this is an issue. Chris -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/5p5p97xkca@news.roaima.co.uk
Re: Bash script - pass command line arg to embedded sed script with multiple args
* 2010-04-17 09:34 (-0700), Michael Elkins wrote: You can run into that sort of problem if your pattern to replace contains any forward slashes (/) in it. If you need to such an expansion, you probably want to do it in two passes, first doing a / to \/ substitution on your replacement strings, then inserting them into your final expression: pata=`echo $1 | sed 's,/,\/,'` patb=`echo $2 | sed 's,/,\/,'` sed -e s/PATTERN1/$pata/ -e s/PATTERN2/$patb/ input output There are more special characters which may need escaping. The following example may be unnecessarily verbose for a throw-away script but it tries to address the escaping problem properly and adds some abstraction. #!/bin/sh input=input output=output sub1=$1 sub2=$2 quote_basic_regexp () { sed -e 's,[][\^$.*],\\,g'; } quote_replace_string () { sed -e 's,[\],\\,g'; } quote_solidus (){ sed -e 's,/,\\,g'; } replace_literal_string () { local regexp=$(printf '%s\n' $1 | quote_basic_regexp | \ quote_solidus) local replace=$(printf '%s\n' $2 | quote_replace_string | \ quote_solidus) sed -e s/$regexp/$replace/g } replace_literal_string PLACEHOLDER1 $sub1 $input | \ replace_literal_string PLACEHOLDER2 $sub2 $output -- Feel free to Cc me your replies if you want to make sure I'll notice them. I can't read all the list mail. -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/87vdbqdjqa@mithlond.arda
Bash script - pass command line arg to embedded sed script with multiple args
What I'm trying to do is pretty simple. Getting it to work is turning out not to be. What I want to do is call a bash script with a couple of arguments, and, within the script, call sed to use those args to replace two placeholders in a file: bashscript SUB1 SUB2 This line inside bashscript doesn't work: sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' input output It doesn't work because the command line args ($1, $2) are quoted and don't get replaced. However, because I'm using the -e argument to sed, I have to quote the argument or sed gets all huffy. I've tried all sorts of variations on the quoting and either bash or sed isn't happy no matter what I do. The only workaround I've found is to do the substitutions in two passes: sed s/SUB1/$1/ input temp sed s/SUB2/$2/ temp output Since I'm not using multiple argument to sed, I don't have to quote the arg. That's an ugly hack and really rubs me the wrong way, however, and it'll get really ugly if I end up having to do more than two substitutions, which I expect to do at a later date. I suppose I could pipe one sed command to another rather than using a temp file but that's not significantly more palatable. Any ideas on a cleaner approach on how to do this, either by getting this right or using an alternative method, are welcome. One thing to note is that this is a small part of a larger, more complex script, so the bash script can't (easily) go away. I don't have to use sed to do the replacement if there's another approach from within the bash script that would work. -- Americans detest all lies except lies spoken in public or printed lies. - Edgar Watson Howe -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/201004162015.38878.ddjo...@riddlemaster.org
Re: Bash script - pass command line arg to embedded sed script with multiple args
On Fri, Apr 16, 2010 at 08:15:38PM -0400, Daniel D Jones wrote: What I'm trying to do is pretty simple. Getting it to work is turning out not to be. What I want to do is call a bash script with a couple of arguments, and, within the script, call sed to use those args to replace two placeholders in a file: bashscript SUB1 SUB2 This line inside bashscript doesn't work: sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' input output If you switch the single quotes to double quotes it will work as you expect. Variables inside of double quotes are expanded. Single quotes are for literal strings, as you've discovered. me pgp8QZgOEKySf.pgp Description: PGP signature
Re: bash or sed script
Le Tue, 07 Jun 2005 16:39:13 +0200, mess-mate a écrit : Ce dernier script marche à MERVEILLE. Tous les anciens fichiers sont enlevés et un nouveau du jour est crée; si un nouveau du jour existe déjà celui-ci est remplacé ! Reste encore: - si plusieurs répertoires à sauver. - que le script s'exécute lors du login à la machine par l'utilisateur (workstation). AMHA, logrotate devrait pouvoir faire le job: ne garder que N fichiers compresser lancer la tâche avec crontab C'est juste une idée Jean-Pierre -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
J.Pierre Pourrez [EMAIL PROTECTED] wrote: | Le Tue, 07 Jun 2005 16:39:13 +0200, mess-mate a écrit : | | Ce dernier script marche à MERVEILLE. Tous les anciens fichiers sont | enlevés et un nouveau du jour est crée; si un nouveau du jour existe | déjà celui-ci est remplacé ! Reste encore: | - si plusieurs répertoires à sauver. - que le script s'exécute lors du | login à la machine par l'utilisateur (workstation). | | AMHA, logrotate devrait pouvoir faire le job: | ne garder que N fichiers | compresser | lancer la tâche avec crontab | | C'est juste une idée | Jean-Pierre La crontab exécute à une certaine heure, non ? Donc pas utilisable pour mon cas. En outre pour sauvegarder des rép. comm par ex. /etc il faut les permissions root. Comment faire ? mess-mate -- Beware of a tall blond man with one black shoe. -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
mess-mate a écrit, mercredi 8 juin 2005, à 13:51 : [...] La crontab exécute à une certaine heure, non ? Donc pas utilisable pour mon cas. Avec anacron, alors ? En outre pour sauvegarder des rép. comm par ex. /etc il faut les permissions root. Comment faire ? Sous root, ou avec sudo. -- Jacques L'helgoualc'h -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
Jacques L'helgoualc'h [EMAIL PROTECTED] wrote: | mess-mate a écrit, mercredi 8 juin 2005, à 13:51 : | [...] | La crontab exécute à une certaine heure, non ? | Donc pas utilisable pour mon cas. | | Avec anacron, alors ? | | En outre pour sauvegarder des rép. comm par ex. /etc il faut les | permissions root. | Comment faire ? | | Sous root, ou avec sudo. | -- D'accord je vais voire la man de anacron. Mais un sudo ne marche pas DANS un script ? (pas pour exécuter le script ) Pour être plus clair: j'ai donc plusieurs rép. à sauvegarder de mon HOME plus au moins le /etc qui lui est sous root. mess-mate -- Q: What do Winnie the Pooh and John the Baptist have in common? A: The same middle name. -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
Bonjour, Je ne comprends pas bien le concept, pourquoi ne pas faire tourner cela sous root via une tche cron classique excut au reboot ? mess-mate a crit: Jacques L'helgoualc'h [EMAIL PROTECTED] wrote: | mess-mate a crit, mercredi 8 juin 2005, 13:51 : | [...] | La crontab excute une certaine heure, non ? | Donc pas utilisable pour mon cas. | | Avec anacron, alors ? | | En outre pour sauvegarder des rp. comm par ex. /etc il faut les | permissions root. | Comment faire ? | | Sous root, ou avec sudo. | -- D'accord je vais voire la man de anacron. Mais un sudo ne marche pas DANS un script ? (pas pour excuter le script ) Pour tre plus clair: j'ai donc plusieurs rp. sauvegarder de mon HOME plus au moins le /etc qui lui est sous root. mess-mate -- Q: What do Winnie the Pooh and John the Baptist have in common? A: The same middle name. -- Frdric LEGER (Siderlog) http://www.siderlog.fr/
Re: bash or sed script
Frédéric LEGER a écrit, mercredi 8 juin 2005, à 17:06 : Bonjour, bonjour, Je ne comprends pas bien le concept, pourquoi ne pas faire tourner cela sous root via une tâche cron classique exécuté au reboot ? C'est justement ce que fait anacron, rattraper le boulot en retard quand on rallume la machine (ça peut être un peu casse-pieds quand on revient chez soi en début de semaine et de mois :) mess-mate a écrit : [...] Mais un sudo ne marche pas DANS un script ? Bin si, « sudo commande », avec l'option NOPASSWD: pour autoriser le lancement de commande (inutile dans le sens root--user). Man sudoers. (pas pour exécuter le script ) Pour être plus clair: j'ai donc plusieurs rép. à sauvegarder de mon HOME plus au moins le /etc qui lui est sous root. Bon, c'est plutôt à root de faire la sauvegarde, alors ... tu peux aussi séparer, d'autant que c'est un trou de sécurité (selon l'umask de root). root # tar cf trou.tar /etc/sudoers /etc/shadow tar: Retrait de l'en-tête `/' des noms des membres root # l trou.tar -rw-r--r--1 root root10240 jun 8 17:31 trou.tar root # tar tvf trou.tar -r--r- root/root 310 2001-07-07 18:38:45 etc/sudoers -rw-r- root/shadow1088 2005-04-29 15:34:42 etc/shadow lhh $ tar xvf /root/trou.tar etc/sudoers etc/shadow lhh $ ls -l etc total 8 -rw-r-1 lhh root 1088 avr 29 15:34 shadow -r--r-1 lhh root 310 jui 7 2001 sudoers -- Jacques L'helgoualc'h -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
Frédéric LEGER [EMAIL PROTECTED] wrote: | Bonjour, | | Je ne comprends pas bien le concept, pourquoi ne pas faire tourner cela sous root via une | tâche cron classique exécuté au reboot ? | | mess-mate a écrit : | | Jacques L'helgoualc'h [EMAIL PROTECTED] wrote: | | mess-mate a écrit, mercredi 8 juin 2005, à 13:51 : | | [...] | | La crontab exécute à une certaine heure, non ? | | Donc pas utilisable pour mon cas. | | Avec anacron, alors ? | | | En outre pour sauvegarder des rép. comm par ex. /etc il faut les | | permissions root. | | Comment faire ? | | | Sous root, ou avec sudo. | | -- D'accord je vais voire la man de anacron. | Mais un sudo ne marche pas DANS un script ? | (pas pour exécuter le script ) | Pour être plus clair: j'ai donc plusieurs rép. à sauvegarder de mon | HOME plus au moins le /etc qui lui est sous root. Oui, c'est vrai, j'y ai pas pensé. Mais comment fait-on pour que le cron ne l'exécute q'au boot ? mess-mate -- Q: How many elephants can you fit in a VW Bug? A: Four. Two in the front, two in the back. Q: How can you tell if an elephant is in your refrigerator? A: There's a footprint in the mayo. Q: How can you tell if two elephants are in your refrigerator? A: There's two footprints in the mayo. Q: How can you tell if three elephants are in your refrigerator? A: The door won't shut. Q: How can you tell if four elephants are in your refrigerator? A: There's a VW Bug in your driveway. -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
mess-mate [EMAIL PROTECTED] wrote: | Frédéric LEGER [EMAIL PROTECTED] wrote: | | Bonjour, | | | | Je ne comprends pas bien le concept, pourquoi ne pas faire tourner cela sous root via une | | tâche cron classique exécuté au reboot ? | | | | mess-mate a écrit : | | | | Jacques L'helgoualc'h [EMAIL PROTECTED] wrote: | | | mess-mate a écrit, mercredi 8 juin 2005, à 13:51 : | | | [...] | | | La crontab exécute à une certaine heure, non ? | | | Donc pas utilisable pour mon cas. | | Avec anacron, alors ? | | | | En outre pour sauvegarder des rép. comm par ex. /etc il faut les | | | permissions root. | | | Comment faire ? | | | | Sous root, ou avec sudo. | | | -- D'accord je vais voire la man de anacron. | | Mais un sudo ne marche pas DANS un script ? | | (pas pour exécuter le script ) | | Pour être plus clair: j'ai donc plusieurs rép. à sauvegarder de mon | | HOME plus au moins le /etc qui lui est sous root. | Oui, c'est vrai, j'y ai pas pensé. | Mais comment fait-on pour que le cron ne l'exécute q'au boot ? | Bien, c'est fait. Lire la man mess-mate ! C'est dommage qu'il n'existe que l'option @reboot dans la crontab. Ca prends pas mal de temps malgré une machine très rapide. Merci à tous pour votre aide ! A titre de revanche :-) mess-mate -- Conscience doth make cowards of us all. -- Shakespeare -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
mess-mate a écrit, mardi 7 juin 2005, à 00:42 : Bonsoir, bonjour, comme j'ai pas d'expérience avec l'extraction d'une partie de mot d'un mot, voici mon pb: J'ai un fichier Boot-07.tgz de ce fichier je voudrais y retrouver le '07' qui représente le n° du jour $ FILE=Boot-07.tgz $ NUM=${FILE#Boot-} $ NUM=${NUM.tgz} Avec un champ de longueur fixe, on a aussi ${paramètre:début:longueur} ; le suivant est numero=$(printf '%02d' $[ $NUM + 1 ]) $ echo $numero 08 et si il est plus petit que celui que je vais créer au jour '08', qu'il soit effacé. Il est évident si plus facile que je pourrais aussi bien créer le fichier '07-boot.tgz' Ou tout simplement numéroter en continu ( plus le n° du jour) ce fichier.Càd 1-boot.tgz, 2-boot.tgz Il me semble plus commode d'utiliser la date, avec un format assurant l'ordre chronologique ; on évite ainsi de dépendre de la présence de l'archive précédente pour déterminer le numéro. $ date '+%Y-%m-%d' 2005-06-07 et la date d'hier est $ date '+%Y-%m-%d' -d '1 day ago' 2005-06-06 et effacer le fichier avec le plus petit n° De ce fait j'aurais toujours qu'un seul fichier de backup qui passera par un crontab. Il me paraît plus sûr d'attendre la création de l'archive du jour pour effacer les précédentes, si la place n'est pas un problème. Le script suivant devrait faire à peu près ce que tu demandes ? ARCHIVE_DU_JOUR=Boot-$(date '+%Y-%m-%d').tgz tar czf $ARCHIVE_DU_JOUR /les/reps/à/sauver { \ ls -1 Boot-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].tgz | \ grep -v -F $ARCHIVE_DU_JOUR | \ xargs rm -f } -- Jacques L'helgoualc'h -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
Jacques L'helgoualc'h [EMAIL PROTECTED] wrote: | mess-mate a écrit, mardi 7 juin 2005, à 00:42 : | Bonsoir, | | bonjour, | | comme j'ai pas d'expérience avec l'extraction d'une partie de mot | d'un mot, voici mon pb: | J'ai un fichier Boot-07.tgz | de ce fichier je voudrais y retrouver le '07' qui représente le n° | du jour | | $ FILE=Boot-07.tgz | | $ NUM=${FILE#Boot-} | $ NUM=${NUM.tgz} | | Avec un champ de longueur fixe, on a aussi ${paramètre:début:longueur} ; | le suivant est | | numero=$(printf '%02d' $[ $NUM + 1 ]) | $ echo $numero | 08 | | | et si il est plus petit que celui que je vais créer au jour '08', | qu'il soit effacé. | Il est évident si plus facile que je pourrais aussi bien créer le | fichier '07-boot.tgz' | | Ou tout simplement numéroter en continu ( plus le n° du jour) ce | fichier.Càd 1-boot.tgz, 2-boot.tgz | | Il me semble plus commode d'utiliser la date, avec un format assurant | l'ordre chronologique ; on évite ainsi de dépendre de la présence de | l'archive précédente pour déterminer le numéro. | | $ date '+%Y-%m-%d' | 2005-06-07 | | et la date d'hier est | | $ date '+%Y-%m-%d' -d '1 day ago' | 2005-06-06 | | et effacer le fichier avec le plus petit n° | De ce fait j'aurais toujours qu'un seul fichier de backup qui | passera par un crontab. | | Il me paraît plus sûr d'attendre la création de l'archive du jour pour | effacer les précédentes, si la place n'est pas un problème. | | | Le script suivant devrait faire à peu près ce que tu demandes ? | | ARCHIVE_DU_JOUR=Boot-$(date '+%Y-%m-%d').tgz | | tar czf $ARCHIVE_DU_JOUR /les/reps/à/sauver { \ | ls -1 Boot-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].tgz | \ | grep -v -F $ARCHIVE_DU_JOUR | \ | xargs rm -f | } | | -- | Jacques L'helgoualc'h Ce dernier script marche à MERVEILLE. Tous les anciens fichiers sont enlevés et un nouveau du jour est crée; si un nouveau du jour existe déjà celui-ci est remplacé ! Reste encore: - si plusieurs répertoires à sauver. - que le script s'exécute lors du login à la machine par l'utilisateur (workstation). C'est trop demandé ? Déjà merci pour ton temps. mess-mate -- A few hours grace before the madness begins again. -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: bash or sed script
mess-mate a écrit, mardi 7 juin 2005, à 16:39 : Jacques L'helgoualc'h [EMAIL PROTECTED] wrote: [...] | Le script suivant devrait faire à peu près ce que tu demandes ? | | ARCHIVE_DU_JOUR=Boot-$(date '+%Y-%m-%d').tgz | | tar czf $ARCHIVE_DU_JOUR /les/reps/à/sauver { \ | ls -1 Boot-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].tgz | \ | grep -v -F $ARCHIVE_DU_JOUR | \ | xargs rm -f | } | Ce dernier script marche à MERVEILLE. Tous les anciens fichiers sont enlevés et un nouveau du jour est crée; si un nouveau du jour existe déjà celui-ci est remplacé ! Reste encore: - si plusieurs répertoires à sauver. tar czf $ARCHIVE_DU_JOUR /rep1/à/sauver /rep2 /rep3 fichier1 fichier2 ... { \ Tar a aussi une option --files-from FICHIER_LISTE. - que le script s'exécute lors du login à la machine par l'utilisateur (workstation). Heu, tu risques de te loger plusieurs fois ? Je mettrais plutôt ça dans un cron quotidien --- ou en refusant de le faire deux fois : ARCHIVE_DU_JOUR=... cd /rep/des/archives/ \ if [ ! -f $ARCHIVE_DU_JOUR ]; then tar czf ... # etc fi et l'appel du script dans ~/.bash_profile, ~/.bash_login, ou ~/.profile, cf. INVOCATION dans man bash. Vérifie aussi si ça marche avec un [gkw]dm quelconque ... C'est trop demandé ? Bah non. Déjà merci pour ton temps. de rien, -- Jacques L'helgoualc'h -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
bash or sed script
Bonsoir, comme j'ai pas d'expérience avec l'extraction d'une partie de mot d'un mot, voici mon pb: J'ai un fichier Boot-07.tgz de ce fichier je voudrais y retrouver le '07' qui représente le n° du jour et si il est plus petit que celui que je vais créer au jour '08', qu'il soit effacé. Il est évident si plus facile que je pourrais aussi bien créer le fichier '07-boot.tgz' Ou tout simplement numéroter en continu ( plus le n° du jour) ce fichier.Càd 1-boot.tgz, 2-boot.tgz et effacer le fichier avec le plus petit n° De ce fait j'aurais toujours qu'un seul fichier de backup qui passera par un crontab. Meric d'avance au spécialistes. mess-mate -- You prefer the company of the opposite sex, but are well liked by your own. -- Pensez à lire la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Pensez à rajouter le mot ``spam'' dans vos champs From et Reply-To: To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: Problem mit Variable in sed-Script
Am 2005-03-01 01:49:18, schrieb Andreas Schmidt: Hallo, ich will per Script eine Zeile aendern lassen. Der Suchstring ist in einer Variablen gespeichert: SEARCH=^AllowUsers.*$ cat $CONFFILE | sed -e 's/$SEARCH/AllowUsers $NAMES/' ^^ Habe aber gerade feststellen muessen, dass das nicht funktioniert -- kein Wunder, sed sucht jetzt wohl nach einem Zeilenende gefolgt vom String SEARCH. Was muss ich tun, damit nach dem Inhalt von $SEARCH gesucht wird? Warum nicht: SEARCH=^AllowUsers.*$ sed -e s/$SEARCH/AllowUsers $NAMES/ $CONFFILE ist einfacherer und sauberer. Schoenen Gruss, Andreas Greetings Michelle -- Linux-User #280138 with the Linux Counter, http://counter.li.org/ Michelle Konzack Apt. 917 ICQ #328449886 50, rue de Soultz MSM LinuxMichi 0033/3/8845235667100 Strasbourg/France IRC #Debian (irc.icq.com) signature.pgp Description: Digital signature
Re: Problem mit Variable in sed-Script
On 2005.03.01 09:42, Michelle Konzack wrote: Warum nicht: SEARCH=^AllowUsers.*$ sed -e s/$SEARCH/AllowUsers $NAMES/ $CONFFILE ist einfacherer und sauberer. Bin halt ein kleiner Schmierfink. :-) Ansonsten spricht aber wohl wirklich nichts dagegen, das zu aendern. Schoenen Gruss, Andreas pgp19WhwAtFEY.pgp Description: PGP signature
Problem mit Variable in sed-Script
Hallo, ich will per Script eine Zeile aendern lassen. Der Suchstring ist in einer Variablen gespeichert: SEARCH=^AllowUsers.*$ cat $CONFFILE | sed -e 's/$SEARCH/AllowUsers $NAMES/' Habe aber gerade feststellen muessen, dass das nicht funktioniert -- kein Wunder, sed sucht jetzt wohl nach einem Zeilenende gefolgt vom String SEARCH. Was muss ich tun, damit nach dem Inhalt von $SEARCH gesucht wird? Schoenen Gruss, Andreas pgpMIqiF7zJMg.pgp Description: PGP signature
Re: Problem mit Variable in sed-Script
On Tue, Mar 01, 2005 at 01:49:18AM +0100, Andreas Schmidt wrote: Hallo, ich will per Script eine Zeile aendern lassen. Der Suchstring ist in einer Variablen gespeichert: SEARCH=^AllowUsers.*$ cat $CONFFILE | sed -e 's/$SEARCH/AllowUsers $NAMES/' cat $CONFFILE | sed -e s/$SEARCH/AllowUsers $NAMES/ Die Shell muss doch an die Variablen ran. tschazu erkan P.S.: Ja es geht sauberer -- über den grenzen muß die freiheit wohl wolkenlos sein -- Haeufig gestellte Fragen und Antworten (FAQ): http://www.de.debian.org/debian-user-german-FAQ/ Zum AUSTRAGEN schicken Sie eine Mail an [EMAIL PROTECTED] mit dem Subject unsubscribe. Probleme? Mail an [EMAIL PROTECTED] (engl)
Re: Problem mit Variable in sed-Script
Guten Morgen! On 2005.03.01 04:45, erkan yanar wrote: On Tue, Mar 01, 2005 at 01:49:18AM +0100, Andreas Schmidt wrote: Hallo, ich will per Script eine Zeile aendern lassen. Der Suchstring ist in einer Variablen gespeichert: SEARCH=^AllowUsers.*$ cat $CONFFILE | sed -e 's/$SEARCH/AllowUsers $NAMES/' cat $CONFFILE | sed -e s/$SEARCH/AllowUsers $NAMES/ Die Shell muss doch an die Variablen ran. Ohja...stimmt. So offensichtlich, jetzt, wo ich nach dem Aufstehen mit der Nase drauf gestossen werde! Danke, jetzt laeuft es... Schoenen Gruss, Andreas pgpe4QyR9oDwH.pgp Description: PGP signature
Sed script
Hi Not debian related but i need help nonetheless. i need to extract the number 997841138254, or any other number from that position, from an hl7 file. The file will look like this : MSH...OBR|0001||997841138254|..F I can use OBR as the starting point of the search and then use the | to move to the begginning of the number... What would be the best way of doing this.. maybe i can use a vi script to search and extract the number for me. I don't know how to select a segment of a line and write that to a file though.. i am not too familiar with sed although i would like to be as it seems rather powerful :) Any suggestions would be appreciated. thanx
Re: Sed script
On Fri, Oct 22, 1999 at 04:28:29PM +1300, [EMAIL PROTECTED] wrote: Hi Not debian related but i need help nonetheless. i need to extract the number 997841138254, or any other number from that position, from an hl7 file. The file will look like this : MSH...OBR|0001||997841138254|..F I can use OBR as the starting point of the search and then use the | to move to the begginning of the number... What would be the best way of doing this.. maybe i can use a vi script to search and extract the number for me. I don't know how to select a segment of a line and write that to a file though.. i am not too familiar with sed although i would like to be as it seems rather powerful :) Any suggestions would be appreciated. Not sed, but: #!/usr/bin/perl -w while() { @array = split /\|/, $_; print $array[3]\n if $#array 3; } You might have to change the subscipts. Use it like $ ./mysplit.pl myfile.txt -- ++ | Eric G. Milleregm2@jps.net | | GnuPG public key: http://www.jps.net/egm2/gpg.asc | ++
Re: Sed script
Duh, even easier: $ cut --delimiter='|' --fields=N infile outfile where N = the fields you want. See man cut. -- ++ | Eric G. Milleregm2@jps.net | | GnuPG public key: http://www.jps.net/egm2/gpg.asc | ++
Re: Sed script
Hi all, i need to extract the number 997841138254, or any other number from that position, from an hl7 file. The file will look like this : MSH...OBR|0001||997841138254|..F I can use OBR as the starting point of the search and then use the | to move to the begginning of the number... Ok here is an idea how to do it in awk. awk -v FS=| '{ if ( $1 ~ /OBR/ ) print $4; }' Could ofcourse be made more advanced if the mumber of | preceeding ORB is changing. Runs on the command line or inside vim (vi). In vim: :%! awk -v FS=| '{ if ( $1 ~ /OBR/ ) print $4; }' Hope it helped Best regards Joakim