Outils pour utilisateurs

Outils du site


issue82:command_and_conquer

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
issue82:command_and_conquer [2014/05/03 18:04] – créée andre_domenechissue82:command_and_conquer [2014/06/18 18:02] (Version actuelle) auntiee
Ligne 1: Ligne 1:
-aLast month I received an email from John, a reader of C&C. He had turned to me for advice on using Sed to insert semi-colons within the text file created by Task WarriorThe reason he wanted to do this was to use the conkytext script to format the To-Do list nicely for his Conky. Included in the email was the file as created by Task WarriorWe then spent a couple of days putting together functioning Sed script (and going through a few format changes), and the end result was an excellent basis for an articleHopefully by the end of this article, the reader will have an idea how to approach Sed expressions in order to tackle tasks that may at first seem complex.+**Last monthwrote an article on using regular expressions within Sed, in an attempt to show my process when creating such expressionsJust before writing this month's article, I received an email from a reader, who shared his solution to the problem (formatting a TaskWarrior file)His solution consisted of using single Sed statement, and addressing lines of the file specificallyHis sed command is shown in the box above right.**
  
-The Task+Le mois dernier, j'ai écrit un article sur l'utilisation des expressions régulières avec Sed, en tentant de vous expliquer comment je m'y prenais pour créer de telles expressions. Juste avant d'écrire l'article de ce mois-ci, j'ai reçu le courriel d'un lecteur qui a partagé sa solution au problème (le formatage d'un fichier pour TaskWarrior). Sa solution consiste à utiliser une seule instruction Sed et à traiter spécifiquement les lignes du fichier. Sa commande Sed est indiquée dans l'encart ci-dessus à droite.
  
-We want to add a semi-colon after the contents of every column (in the text shown top right, ignoring the white space). As you can imagine, the fact that the number of spaces vary can make this a difficult task. Also, the last line (tasks) is supposed to be preceded by three semi-colons (“;;;10 tasks”). After our first attempt, John came back to me and told me he'd decided to leave the first column semi-colon-less (shown above). 
  
-My Script+**His explanation: 
 +• Take care of this one special case with the title line. As there is an address label (1), this substitution is done only for line 1. In sed scripts, a line number matches only that line. I used an address label only for performance purposes - there would be just one single line matching "^ID", 
 +• Another address label (2). This rule is used only for line 2, and the command is branch ("goto"). As there is no label, it means "goto to the end of the script" - simply skip line 2. This is extraneous for the sake of an example. 
 +• The third line matches only the "tasks" line. 
 +• The fourth line takes care of the semicolons after the dates, as there will never be two spaces due to right-adjustment. Note that you can also use delimiters other than slashes. 
 +• Finally, take care of the rest. Substitute each at-least-two space combinations with a semicolon and the spaces found. This also applies to the first line (headline).**
  
-Due to the fact that the script is rather long, as it offers extra functionality (supports some arguments, outputting to a file, etc), I've put it up on pastebinhttp://pastebin.com/SHTVjDTM.+Son explication :
  
-The Thought Process+• Prenez note du cas particulier concernant la ligne de titre, où la présence de l'étiquette d'adresse (1) indique que cette substitution ne s'applique qu'à la ligne 1. Dans les scripts sed, indiquer un numéro de ligne a pour effet de s'appliquer uniquement à cette ligne. J'ai utilisé une étiquette d'adresse seulement à des fins de performance, car il n'y aurait qu'une seule ligne unique correspondant à « ^ ID ». 
  
-There are a few things worth noting before we begin: +• Une autre étiquette d'adresse (2est utilisée pour ne s'appliquer qu'à la ligne 2 et elle signifie d'aller à la fin du script (« goto »: c'est comme cela que se comporte sed lorsque l'on omet l'étiquette - du coup on saute la ligne 2C'est un peu superflu icimais c'était pour l'exemple... 
-• The typical format of a sed command is: sed s/<search>/<replace>/+
-Sed calls replace “substitute”, hence the s at the beginning. The left hand side (LHSis the search section – here you declare what it is you're trying to match. The right hand side (RHSis the replace section – here you tell Sed what the found line should look like afterwards. The “g” at the end tells Sed to replace all instances (as it would otherwise quit after the first match). +
-• Putting anything in \(\) will allow you to refer back to it on the RHS of the expression. +
-There are certain special characters that can be used in sed. We mainly need the “\s” expressionwhich stands for any space. +
-• Declaring a set number of repetitions can be done with: \{3\} for 3 repetitions, \{3,\} for three or more, and \{3,6\} for three to six. +
-• You must escape the semi-colon.+
  
-Some tips as to how I decide on each expression: +• La troisième ligne correspond uniquement à la ligne « tasks »
-• Figure out where you need to insert the character, as that defines where you group (in our case before the spaces, hence the second group is almost always started before the space character) +
-• Work bit-by-bit. Start with a simple sed command like: sed -e "s/^[0-9]*/FC/g" (FC for first column). This just matches any line started with a number, and replaces it with “FC”, so you can visually check what is being matched. Doing so let me realize that all single-digit ID's started with a space, and helped form an expression for it. It's not included in the actual file, since our end formatting has changed since then. Once you have a working command for the task you outlined, you can move onto a second expression. +
-• If you have issues with step 2 because you can't get the regular expressions working, try using grep and the same regular expression. This lets you rule out the expression itself being wrong, and indicates it's a quirk of Sed's you haven't accounted for yet. +
-• If you want the same formatting at the end, the RHS of the expression should almost always be the same, and if it isn't, it's an indicator that you're either going too complicated, or the chunk you're working on is too big, so try to break it down some more.+
  
-The Expressions+• La quatrième ligne traite les points-virgules après les dates en sachant qu'il n'y aura jamais deux espaces en raison de l'alignement à droite. Notez que vous pouvez également utiliser des délimiteurs autres que des barres obliques. 
  
-first_expression="s/\([a-zA-Z0-9]\)\(\s\{2,15\}\)/\1\;\2/g" +• Enfin, pour traiter le reste, remplacez toutes les combinaisons de doubles espaces (et pluspar un point-virgule. Cela s'applique également à la première ligne de titre.
-second_expression="s/\([0-9]\{3\}\)\(\s[a-zA-Z0-9]\)/\1\;\2/g"+
  
-third_expression="s/\([a-zA-Z]\)\(\s[0-9]\{1,2\}\/\)/\1\;\2/g"+**His solution is certainly more efficient than mineand is a brilliant example of how there are many solutions to these sorts of problems.**
  
-fourth_expression="s/\(^[0-9]*\stasks\)/\;\;\;\1/g"+Sa solution est certainement plus efficace que la mienne, et c'est un brillant exemple qui prouve qu'il existe différentes manières de traiter ce genre de problème.
  
-fifth_expression="s/\(^[A-Z]*\)\(\s*[a-zA-Z]\)/\1\;\2/g" # Check for any number of capital letters at the start of a line, followed by a space and more text, and insert a semicolon. 
  
-The explanations+**Due to the fact that work has kept me extremely busy the last few weeks, I have decided to not write a typical article for this month. Instead, I'd like to run a vote on what article the readers would like to see in FCM#84. The reason why it will appear only in FCM#84, is due to the time frame between FCM being released, and my next article being due. The choices are as follows: 
 +• A reader has requested an in-depth article on installing & setting up Rails 4.0.2 on Ubuntu (Ruby on Rails) 
 +• I recently installed ArchLinux to an external hard drive, capable of running on UEFI systems (Windows 8 or Mac OS X machines, mainly) 
 +• Last month I also offered to set up an article with formatting problems to be solved using regular expressions and sed.**
  
-The first expression tells Sed “Look for any character (a-z, A-Z, or 0-9)and see if it's followed by 2 or more spaces, then add a semi-colon before the spaces”The trick to this is knowing that Sed can group matches to the regular expressions. This is why we have escaped brackets around the expressions. “\(a-zA-Z0-9]\)” then becomes match “\1” in the replacement section of Sed. We are essentially forming two groups – the character that precedes the spacesand the spaces themselves. Then, in the replacement step, we're inserting a semi-colon between the two groupsThis corresponds to column 2 and column 4 in our file, as well as all the headers except ID. The reason why ID isn't included is due to the fact that we state 2 or more spaces, and changing that to one or more would cause issues in all the descriptionsNoteThe semi-colon must be escaped (have a backslash in front of it). Also, if you want to match more than 15 spaces, simply leave that side of the comma empty - \{2,\}.+Puisque mon travail me tenait très occupé ces dernières semainesj'ai décidé de ne pas écrire un article « normal » pour ce mois-ciAu lieu de celaje voudrais lancer un sondage à propos de l'article que les lecteurs aimeraient voir dans le FCM n° 84La raison pour laquelle cet article n’apparaîtra que dans le FCM n° 84 est dû au laps de temps qui existe entre la sortie d'un numéro et la date à laquelle je dois rendre mon articleLes choix sont les suivants
  
-The second expression tells Sed “Look for any 3 consecutive digits that are followed by space and a letter or number, then insert a semi-colon”. What this matches is the date – the format of the date is always going to be so long that only one space is inserted between columns. Naturally, you could check for any number of spaces, but that could cause issues if you use numbers in your Projects. This will apply to any format of date where the year is at the endThis handles column 3 in our file.+• Un lecteur demandé un article détaillé sur l'installation et la mise en place de Rails 4.0.2 sous Ubuntu (Ruby on Rails) 
  
-The third expression can be translated as “Find all letters followed by a 1 or 2 digit numberfollowed by a slashand insert the semi-colon.” The only column that contains a slash is our formatted date column – this applies therefore to the column before it (Project). The reason why I didn't include numbers in this case, is because the second expression could handle this if you tell Sed to accept any number of spaces after the 3 digits. This handles column 2 in our file.+• J'ai récemment installé ArchLinux sur un disque dur externecapable de fonctionner sur les systèmes UEFI ( Windows 8 ou Mac OS X machinesprincipalement
  
-The fourth expression handles the last line of the fileand inserting the 3 semi-colons before tasks. It essentially groups the entire line (10 tasks) and then inserts three semi-colons before that group. If you're adding semi-colons before any lines starting with numbers, then you should move this expression to the start of the list of expressions, so Sed doesn't match it.+• Le mois dernierj'ai également proposé de mettre en place un article avec des problèmes de mise en forme à résoudre en utilisant des expressions régulières et sed.
  
-The fifth expression simply states “Find the line that starts with any number of capital letters, and insert a space afterwards”. I go a little more specific, and state “followed by any number of spaces and more letters”. However, it's not necessary in our example, and is simply there to be a bit more robust. 
  
-That about covers the steps I undertook in this scenario. I realize that this is relatively specific occasion, and not everyone will want to have this exact formattingMy hope is that following my process will help you understand how to approach these sorts of problemsIf it's wished for, I can spend an article focusing on short formatting problems, and working through it step by stepIf anyone is interested in that sort of article, please let me know via email. As always, any questions/concerns or requests can be directed to me at lswest34+fcm@gmail.com.+**Naturally, anyone who has preferred topic not listed above, is welcome to mark the “other” box, and to give me a brief descriptionAnyone is also welcome to input their email address in the form, so that I can contact you with questions about your responseI promise I won't contact you for any other reasonThere is also a secondary paragraph text box where you can expand upon your idea.**
  
-TABLEAUX (à traduire ?)+Naturellement, pour ceux qui ont un sujet préféré qui ne figure pas ci-dessus, leur idée est la bienvenue, et vous pouvez cocher la case « autre » et me donner une brève description de ce que vous souhaiteriez. Toute personne est également invitée à indiquer son adresse e-mail dans le formulaire, afin que je puisse vous contacter pour des questions au sujet de votre réponse. Je promets de ne pas vous contacter pour une autre raison. Une zone de texte où vous pouvez expliciter un peu plus votre idée figure sur le formulaire.
  
-Tableau 1 
  
-Note: I've altered all entries in the file, for the sake of privacy.+**The link to the form: https://docs.google.com/forms/d/1ZqLOwpwZ-iGkU-LVBDkz65pvO8FK65rIF_X2DWGPLmQ/viewform**
  
-ID Project     Due Date   Description                     Due in: +Le lien vers le formulaire: https://docs.google.com/forms/d/1ZqLOwpwZ-iGkU-LVBDkz65pvO8FK65rIF_X2DWGPLmQ/viewform
--- ----------- ---------- ------------------------------- ------- +
- 3 Work        12/10/2013 Work Project                     -8 hrs +
- 6 Work        12/12/2013 Submit 1st draft                 -2 days +
-10 Work        12/15/2013 Prepare Presentation             -5 days +
- 7 University  12/16/2013 Tutorial Class                   -6 days +
- 2 Hobby       12/17/2013 Change Pickups in Strat          -7 days +
- 4 Banking     12/17/2013 Pay the bills                    -7 days +
- 1 Hobby       12/18/2013 Read Daemon                      -8 days +
- 5 Programming 12/31/2013 Update Ruby on Rails Website     -3 wks +
- 8 Work         1/10/2014 Interim Report                   -4 wks +
- 9 Hobby        1/13/2014 Build Blu-Ray stand              -4 wks+
  
-10 tasks 
- 
-Tableau 2 
- 
- 
-ID; Project;     Due Date;   Description;                     Due in: 
--- ----------- ---------- ------------------------------- ------- 
- 3 Work;        12/10/2013; Work Project;                     -8 hrs 
- 6 Work;        12/12/2013; Submit 1st draft;                 -2 days 
-10 Work;        12/15/2013; Prepare Presentation;             -5 days 
- 7 University;  12/16/2013; Tutorial Class;                   -6 days 
- 2 Hobby;       12/17/2013; Change Pickups in Strat;          -7 days 
- 4 Banking;     12/17/2013; Pay the bills;                    -7 days 
- 1 Hobby;       12/18/2013; Read Daemon;                      -8 days 
- 5 Programming; 12/31/2013; Update Ruby on Rails Website;     -3 wks 
- 8 Work;         1/10/2014; Interim Report;                   -4 wks 
- 9 Hobby;        1/13/2014; Build Blu-Ray stand;              -4 wks 
- 
-;;;10 tasks 
  
 +**I apologize for not having a complete article for you this month. However, FCM#83 should contain a normal article next month.
 +**
 +Je m'excuse de ne pas avoir rédigé un article complet pour vous ce mois-ci. Cependant, le mois prochain, le FCM n° 83 devra contenir un article « normal ».
  
issue82/command_and_conquer.1399133065.txt.gz · Dernière modification : 2014/05/03 18:04 de andre_domenech