I have file with the below info
testing
testing
testing
I want to insert a word(tested) before the first testing word using sed or any linu
To provide an awk
-based alternative that is easier to understand:
awk '!found && /testing/ { print "tested"; found=1 } 1' file
found
is used to keep track of whether the first instance of testing
has been found (variable found
, as any Awk variable, defaults to 0
, i.e., false
in a Boolean context)./testing/
therefore matches the first line containing testing
, and processes the associated block:
{ print "tested"; found=1 }
prints the desired text and sets the flag that the first testing
line has been found1
is a common shorthand for { print }
, i.e., simply printing the current input line as is.