London| Elhadj Abdoul Diallo| Module-Tools | WEEK1 - Individual-shell-tools#16
London| Elhadj Abdoul Diallo| Module-Tools | WEEK1 - Individual-shell-tools#16eediallo wants to merge 44 commits into
Conversation
…per-files directory in script-02.sh
…es in helper-files directory in script-04-stretch.sh
…t in script-01.sh
…t in script-02.sh
… files in helper-files directory in script-03.sh
…alogue.txt in script-01.sh
…dless of case) from dialogue.txt in script-02.sh
…nsensitive) in dialogue.txt in script-03.sh
…se insensitive) from dialogue.txt in script-04.sh
…preceding line from dialogue.txt in script-05.sh
…ines spoken by the Doctor
…s spoken by the Doctor (case insensitive)
…octor in all .txt files
…in input.txt using sed
…ut.txt in script-03.sh
…will" in input.txt in script-04.sh
…input.txt in script-05.sh
…xt in script-06.sh
…t in script-01.sh
…mpt score in script-03.sh
…cores in script-06-stretch.sh
… of their scores in script-07-stretch.sh
…' in dialogue.txt in script-03.sh
…he total of their scores in script-07-stretch.sh" This reverts commit 779cb97.
…ue.txt in script-03.sh
…Module-Tools into individual-shell-tools
… of their scores in script-07-stretch.sh
mjpeet
left a comment
There was a problem hiding this comment.
You're doing great with your scripts! Keep experimenting and practicing. Every script you write improves your skills. Don't hesitate to ask for help and keep up the fantastic work!
| # Your output should contain 6 lines, each with one word and one number on it. | ||
| # The first line should be "Ahmed 1". | ||
|
|
||
| awk '{print $1, $3}' scores-table.txt No newline at end of file |
There was a problem hiding this comment.
how would your script look like if you used printf instead of print? what would be the advantage (if any) to use printf?
There was a problem hiding this comment.
That's a good question! I've never tried printf before with awk but I guess it would allow me to format the result but as the content of the file is alreay in text format print is enough in this case
| # The first line should be "Ahmed 4". | ||
| # Output the names of each player in London along with the score from their last attempt. | ||
| # For lines with fewer than 5 columns, print the last column. | ||
| awk '{if (NF >= 5 && $2 == "London") print $1, $5; else if (NF < 5 && $2 == "London") print $1, $NF}' scores-table.txt |
There was a problem hiding this comment.
wow, very nice work! (especially, because if you look at the current the latest changes for this in the repo, the condition for "For lines with fewer than 5 columns, print the last column." was taken out)
| # TODO: Write a command to output just the names of each player along with the total of adding all of that player's scores. | ||
| # Your output should contain 6 lines, each with one word and one number on it. | ||
| # The first line should be "Ahmed 15". The second line should be "Basia 37" | ||
| awk '{ for(i = 3; i <= NF; i++) sum += $i; print $1, sum; sum = 0 }' scores-table.txt No newline at end of file |
There was a problem hiding this comment.
i noticed you set initial value for sum at the end of the block - would it be better to initialise sum in another way? (regardless, your solution does print out the correct solution!)
There was a problem hiding this comment.
Yes, I could have done it at the biginning like this:
awk '{ sum= 0; for(i = 3; i <= NF; i++) sum += $i; print $1, sum}' scores-table.txt| # 1 It looked delicious. | ||
| # 2 I was tempted to take a bite of it. | ||
| # 3 But this seemed like a bad idea... | ||
| cat -n ../helper-files/helper-3.txt |
There was a problem hiding this comment.
how would this script look like if used the | (pipe) ?
There was a problem hiding this comment.
We would have piped it to the nl command like this:
cat ../helper-files/helper-3.txt | nl|
|
||
| # TODO: Write a command to output every line in dialogue.txt that contains the word Doctor (regardless of case). | ||
| # The output should contain 9 lines. | ||
| grep doctor -i dialogue.txt |
There was a problem hiding this comment.
when you look at the grep command man page, where are the grep options "positioned" compared to the parameters? (your solution works, but there may be commands where the order of the options and input(s) matters)
There was a problem hiding this comment.
I agree, going to fix it by following the correct order: grep [OPTION...] PATTERNS [FILE...]
|
|
||
| # TODO: Write a command to output, for each `.txt` file in this directory, how many lines of dialogue the Doctor has. | ||
| # The output should show that dialogue.txt contains 6 lines, dialogue-2.txt contains 2, and dialogue-3.txt contains 0. | ||
| grep ^Doctor -icH *.txt |
There was a problem hiding this comment.
if you need to use the minimum of the options, how would your solution look like? (right now you're using 3)
There was a problem hiding this comment.
I would remove the -H option and the result will still look the same
| # TODO: Write a command which _recursively_ lists all of the files and folders in this directory _and_ all of the files inside those folders. | ||
| # The output should be a list of names including: child-directory, script-01.sh, helper-1.txt (and more). | ||
| # The formatting of the output doesn't matter. | ||
| cd ../ls |
There was a problem hiding this comment.
when you consider where this script (script-03.sh) is located, and where it would be executed, do you think this directory change is necessary?
There was a problem hiding this comment.
It is not neccessary in this case. going to remove it.
| # The output should be a list of names in this order, one per line: helper-3.txt, helper-1.txt, helper-2.txt. | ||
|
|
||
| cd ../ls/child-directory | ||
| ls -1t |
There was a problem hiding this comment.
how would you modify this script if using the command cd is not an option? (do you "need" to use cd ?)
There was a problem hiding this comment.
I can make it work without cd-ing
| # TODO: Write a command to output input.txt with all occurrences of the letter `i` replaced with `I`. | ||
| # The output should contain 11 lines. | ||
| # The first line of the output should be: "ThIs Is a sample fIle for experImentIng with sed.". | ||
| sed s/i/I/g input.txt |
There was a problem hiding this comment.
what would the option -e mean for this script?
There was a problem hiding this comment.
it specifies multiple editing commands in a single sed invocation. But is not needed in this case because there is only one command being executed.
| # If a line starts with a number and a space, make the line instead end with a space and the number. | ||
| # So line 6 which currently reads "37 Alisha" should instead read "Alisha 37". | ||
| # The output should contain 11 lines. | ||
| sed -E 's/^([0-9]+) (.*)$/\2 \1/' input.txt No newline at end of file |
There was a problem hiding this comment.
(optional) how would you change this script if you need to use basic regular expressions? (congrats on using ERE ! )
…oving unnecessary cd
mjpeet
left a comment
There was a problem hiding this comment.
thanks for getting back on the comments!
|
Thank you @mjpeet for the review and constructive feedback. |
Learners, PR Template
Self checklist