Skip to main content
show equivalent shell syntax
Source Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k

In English, this sed program means: for each line,

  • [123!] if the current line number is not 123, then [d] delete the current line and start the next cycle (i.e. move to the next line);
  • then (but we only reach this point if the d command was not executed), [q] exit without processing any more lines (but do print out the current line in our dying throes).

Or if you prefer, in shell syntax:

line_number=0
while IFS= read -r pattern_space; do
  line_number=$(($line_number+1))
  if [ $line_number -ne 123 ]; then       # 123!
    continue                              #   d
  fi
  echo "$pattern_space"; break            # q
  echo "$pattern_space"                   # implicit final print (never reached)
done

In English, this sed program means: for each line,

  • [123!] if the current line number is not 123, then [d] delete the current line and start the next cycle (i.e. move to the next line);
  • then (but we only reach this point if the d command was not executed), [q] exit without processing any more lines (but do print out the current line in our dying throes).

In English, this sed program means: for each line,

  • [123!] if the current line number is not 123, then [d] delete the current line and start the next cycle (i.e. move to the next line);
  • then (but we only reach this point if the d command was not executed), [q] exit without processing any more lines (but do print out the current line in our dying throes).

Or if you prefer, in shell syntax:

line_number=0
while IFS= read -r pattern_space; do
  line_number=$(($line_number+1))
  if [ $line_number -ne 123 ]; then       # 123!
    continue                              #   d
  fi
  echo "$pattern_space"; break            # q
  echo "$pattern_space"                   # implicit final print (never reached)
done
Source Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k

In English, this sed program means: for each line,

  • [123!] if the current line number is not 123, then [d] delete the current line and start the next cycle (i.e. move to the next line);
  • then (but we only reach this point if the d command was not executed), [q] exit without processing any more lines (but do print out the current line in our dying throes).