Bash Scripting - Text Processing in ScriptsHow can you use sed to replace only the second occurrence of 'cat' with 'dog' on each line?Ased 's/cat/dog/g2' file.txtBsed '2s/cat/dog/' file.txtCsed 's/cat/dog/2' file.txtDsed 's/cat/dog/1g' file.txtCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand occurrence flag in sed substitutionAdding a number after the substitution command replaces only that occurrence number per line.Step 2: Identify correct syntax for second occurrencesed 's/cat/dog/2' file.txt uses 's/cat/dog/2' which replaces only the second 'cat' per line.Final Answer:sed 's/cat/dog/2' file.txt -> Option CQuick Check:Number after 's///' targets specific occurrence [OK]Quick Trick: Use number after 's///' to replace nth occurrence only [OK]Common Mistakes:MISTAKESUsing 'g2' which is invalidConfusing line number with occurrence numberAssuming '2s' targets second occurrence
Master "Text Processing in Scripts" in Bash Scripting9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Bash Scripting Quizzes Error Handling - set -e for exit on error - Quiz 8hard Error Handling - set -e for exit on error - Quiz 13medium Functions - Recursive functions - Quiz 7medium Functions - Calling functions - Quiz 4medium Functions - Function definition - Quiz 13medium Functions - Why functions organize reusable code - Quiz 14medium String Operations - String prefix removal (${var#pattern}) - Quiz 10hard String Operations - Substring extraction (${var:offset:length}) - Quiz 9hard String Operations - String length (${#var}) - Quiz 3easy Text Processing in Scripts - Here strings (<<<) - Quiz 7medium