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

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
issue82:command_and_conquer [2014/05/05 17:11] andre_domenechissue82:command_and_conquer [2014/06/18 18:02] (Version actuelle) auntiee
Ligne 1: Ligne 1:
-titre : Sed & Reader Survey+**Last month, I wrote an article on using regular expressions within Sed, in an attempt to show my process when creating such expressions. Just 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 a single Sed statement, and addressing lines of the file specifically. His sed command is shown in the box above right.**
  
-Last month I received an email from Johna 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 Warrior. We then spent couple of days putting together a 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.+Le mois dernierj'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 expressionsJuste avant d'écrire l'article de ce mois-ci, j'ai reçu le courriel d'un lecteur qui 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.
  
-The Task 
  
-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 imaginethe fact that the number of spaces vary can make this a difficult taskAlsothe last line (tasks) is supposed to be preceded by three semi-colons (“;;;10 tasks”)After our first attemptJohn came back to me and told me he'd decided to leave the first column semi-colon-less (shown above).+**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, line number matches only that lineI 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 datesas 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).**
  
-My Script+Son explication :
  
-Due to the fact that the script is rather longas it offers extra functionality (supports some arguments, outputting to a file, etc), I've put it up on pastebin: http://pastebin.com/SHTVjDTM.+• Prenez note du cas particulier concernant la ligne de titreoù la présence de l'étiquette d'adresse (1indique que cette substitution ne s'applique qu'à la ligne 1. Dans les scripts sedindiquer un numéro de ligne a pour effet de s'appliquer uniquement à cette ligneJ'ai utilisé une étiquette d'adresse seulement à des fins de performance, car il n'y aurait qu'une seule ligne unique correspondant à « ^ ID »
  
-The Thought Process+• Une autre étiquette d'adresse (2) est 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 2. C'est un peu superflu ici, mais c'était pour l'exemple... 
  
-There are a few things worth noting before we begin: +• La troisième ligne correspond uniquement à la ligne « tasks »
-• 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 (LHS) is the search section – here you declare what it is you're trying to match. The right hand side (RHS) is 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” expression, which 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 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 à droiteNotez que vous pouvez également utiliser des délimiteurs autres que des barres obliques
-• 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 expressionThis 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+• Enfin, pour traiter le reste, remplacez toutes les combinaisons de doubles espaces (et plus) par un point-virgule. Cela s'applique également à la première ligne de titre.
  
-first_expression="s/\([a-zA-Z0-9]\)\(\s\{2,15\}\)/\1\;\2/g" +**His solution is certainly more efficient than mineand is brilliant example of how there are many solutions to these sorts of problems.**
-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"+Sa solution est certainement plus efficace que la mienneet c'est un brillant exemple qui prouve qu'il existe différentes manières de traiter ce genre de problème.
  
-fourth_expression="s/\(^[0-9]*\stasks\)/\;\;\;\1/g" 
  
-fifth_expression="s/\(^[A-Z]*\)\(\s*[a-zA-Z]\)/\1\;\2/g" # Check for any number of capital letters at the start of linefollowed by space and more text, and insert a semicolon.+**Due to the fact that work has kept me extremely busy the last few weeks, I have decided to not write typical article for this month. InsteadI'd like to run 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 drivecapable 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 explanations+Puisque mon travail me tenait très occupé ces dernières semaines, j'ai décidé de ne pas écrire un article « normal » pour ce mois-ci. Au lieu de cela, je voudrais lancer un sondage à propos de l'article que les lecteurs aimeraient voir dans le FCM n° 84. La 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 article. Les choix sont les suivants: 
  
-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 expressionsThis 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 spaces, and the spaces themselves. Then, in the replacement step, we're inserting a semi-colon between the two groups. This corresponds to column 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 descriptions. Note: The 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,\}.+• 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 second expression tells Sed “Look for any 3 consecutive digits that are followed by a space and a letter or numberthen 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. Naturallyyou 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 end. This handles column 3 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 third expression can be translated as “Find all letters followed by a 1 or 2 digit numberfollowed by a slash, and 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.+• 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 fourth expression handles the last line of the file, and 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. 
  
-The fifth expression simply states “Find the line that starts with any number of capital lettersand insert space afterwards”. I go a little more specificand state followed by any number of spaces and more letters. However, it's not necessary in our example, and is simply there to be bit more robust.+**Naturallyanyone who has preferred topic not listed aboveis welcome to mark the other” box, and to give me a brief description. Anyone is also welcome to input their email address in the form, so that I can contact you with questions about your response. I promise I won't contact you for any other reason. There is also secondary paragraph text box where you can expand upon your idea.**
  
-That about covers the steps I undertook in this scenario. I realize that this is a relatively specific occasionand not everyone will want to have this exact formatting. My hope is that following my process will help you understand how to approach these sorts of problems. If it's wished forI can spend an article focusing on short formatting problemsand working through it step by step. If anyone is interested in that sort of article, please let me know via emailAs alwaysany questions/concerns or requests can be directed to me at lswest34+fcm@gmail.com.+Naturellementpour ceux qui ont un sujet préféré qui ne figure pas ci-dessusleur idée est la bienvenueet vous pouvez cocher la case « autre » et me donner une brève description de ce que vous souhaiteriezToute personne est également invitée à indiquer son adresse e-mail dans le formulaireafin que je puisse vous contacter pour des questions au sujet de votre réponse. Je promets de ne pas vous contacter pour une autre raisonUne zone de texte où vous pouvez expliciter un peu plus votre idée figure sur le formulaire.
  
-TABLEAUX (à traduire ?) 
  
-Tableau 1+**The link to the form: https://docs.google.com/forms/d/1ZqLOwpwZ-iGkU-LVBDkz65pvO8FK65rIF_X2DWGPLmQ/viewform**
  
-NoteI've altered all entries in the file, for the sake of privacy.+Le lien vers le formulairehttps://docs.google.com/forms/d/1ZqLOwpwZ-iGkU-LVBDkz65pvO8FK65rIF_X2DWGPLmQ/viewform
  
-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 
- 
-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.1399302702.txt.gz · Dernière modification : 2014/05/05 17:11 de andre_domenech