I have a question: Write a Linux sed command to save lines that match the pattern “number” (upper or lowercase) for the file: datat.txt and save results (overwriting previous contents) to: ~/results.txt

I use this command:

sed -n -i '/number/p' data.txt>~/results.txt

But when I use more command to look at results.txt file, it showed the empty text file. PLease let me know did I do wrong?

1 Answer 1


you use two options

  • -n : quiet mode, no output
  • -i : in place edit

so you overide original file, keeping only pattern, and outputing nothing.

maybe you intend ?

sed -n '/number/Ip' data.txt > ~/results.txt
  • without -n flag, all line will be echoed
  • I flag for case insensivity

man sed

-n, --quiet, --silent

suppress automatic printing of pattern space

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied). The default operation mode is to break symbolic and hard links. This can be changed with --follow-symlinks and --copy.

  • I was going to save lines which contain the pattern 'number', overwrite the original file and save the final result to a new file name results.txt
    – Alex
    Jul 21, 2022 at 14:33
  • -i : isn't it for ignoring upper/lowercase?
    – Alex
    Jul 21, 2022 at 14:34
  • -i ignore case for grep, not sed.
    – Archemar
    Jul 21, 2022 at 14:44
  • oh, I see. Thank you so much
    – Alex
    Jul 21, 2022 at 14:49
  • Thanks everyone. It works!!! Have a great day ^^
    – Alex
    Jul 21, 2022 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .