issue82:command_and_conquer
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue82:command_and_conquer [2014/05/03 18:04] – créée andre_domenech | issue82:command_and_conquer [2014/06/18 18:02] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | aLast month I received | + | **Last |
- | The Task | + | Le mois dernier, j'ai écrit un article sur l' |
- | 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 " | ||
+ | • Another address label (2). This rule is used only for line 2, and the command is branch (" | ||
+ | • The third line matches only the " | ||
+ | • 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 pastebin: http:// | + | Son explication |
- | The Thought Process | + | • Prenez note du cas particulier concernant la ligne de titre, où la présence de l' |
- | There are a few things worth noting before we begin: | + | • Une autre étiquette d' |
- | • The typical format of a sed command is: sed s/< | + | |
- | Sed calls replace “substitute”, | + | |
- | • 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, | + | |
- | • 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 " | + | |
- | • 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' | + | |
- | • 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, | + | |
- | 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' |
- | first_expression=" | + | • Enfin, pour traiter le reste, remplacez toutes les combinaisons de doubles espaces |
- | second_expression=" | + | |
- | third_expression=" | + | **His solution is certainly more efficient than mine, and is a brilliant example of how there are many solutions to these sorts of problems.** |
- | fourth_expression=" | + | 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=" | ||
- | 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# |
+ | • 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 spaces, and the spaces themselves. Then, in the replacement step, we're inserting a semi-colon between the two groups. This 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 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,\}. | + | 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 second expression tells Sed “Look for any 3 consecutive digits that are followed by a 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 end. This handles column 3 in our file. | + | • Un lecteur |
- | The third expression can be translated as “Find all letters followed by a 1 or 2 digit number, followed 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. | + | • J'ai récemment installé ArchLinux sur un disque dur externe, capable de fonctionner sur les systèmes UEFI ( Windows 8 ou Mac OS X machines, principalement) |
- | 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' | + | • Le mois dernier, j'ai également proposé de mettre en place un article avec des problèmes de mise en forme à résoudre en utilisant des expressions |
- | 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 a relatively specific occasion, and 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 | + | **Naturally, |
- | TABLEAUX (à traduire ?) | + | Naturellement, |
- | Tableau 1 | ||
- | Note: I've altered all entries in the file, for the sake of privacy. | + | **The link to the form: https:// |
- | ID Project | + | Le lien vers le formulaire: https://docs.google.com/forms/d/1ZqLOwpwZ-iGkU-LVBDkz65pvO8FK65rIF_X2DWGPLmQ/viewform |
- | -- ----------- ---------- ------------------------------- ------- | + | |
- | 3 Work 12/10/2013 Work Project | + | |
- | 6 Work 12/12/2013 Submit 1st draft -2 days | + | |
- | 10 Work 12/15/2013 Prepare Presentation | + | |
- | 7 University | + | |
- | 2 Hobby 12/17/2013 Change Pickups in Strat -7 days | + | |
- | 4 Banking | + | |
- | 1 Hobby | + | |
- | 5 Programming 12/31/2013 Update Ruby on Rails Website | + | |
- | 8 Work | + | |
- | 9 Hobby 1/13/2014 Build Blu-Ray stand -4 wks | + | |
- | 10 tasks | ||
- | |||
- | Tableau 2 | ||
- | |||
- | |||
- | ID; Project; | ||
- | -- ----------- ---------- ------------------------------- ------- | ||
- | 3 Work; 12/10/2013; Work Project; | ||
- | 6 Work; 12/12/2013; Submit 1st draft; | ||
- | 10 Work; 12/15/2013; Prepare Presentation; | ||
- | 7 University; | ||
- | 2 Hobby; | ||
- | 4 Banking; | ||
- | 1 Hobby; | ||
- | 5 Programming; | ||
- | 8 Work; | ||
- | 9 Hobby; | ||
- | |||
- | ;;;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' | ||
issue82/command_and_conquer.1399133065.txt.gz · Dernière modification : 2014/05/03 18:04 de andre_domenech