How can I find a word (e.g. "/dn") in a text file and add on the next line another word (e.g. "/period")?

I'd like to execute it using MS DOS.

I mean this "/dn" (after /dn is space) is one word (not a fragment of the text, but the whole word--after /dn can be other word on the same line) which I want to find and then after it on the next new line, not replacing the other lines and other words. I mean create a new line between already existing lines.

For example, if I have this input file:


I want this output:


and "/" symbol should be with dn, not without it.

  • 7
    On the next line? Where on the next line? Should it replace what is currently in the next line? Inserta a new line? Insert the text at the beginning of the next line? Please edit your question, show us a simple example input and the output you want to see from it. – terdon Jul 26 '17 at 10:54
  • Please edit your question and show us an example of your input and your desired output. – terdon Jul 26 '17 at 12:50
  • and "/" symbol should be with "dn", not without it – Lunsy Jul 26 '17 at 12:50
  • Thanks. So, will /dn always be the only word in the line? Can you have more than one /dn on the same line? What if you have /dna, does that count? Can you have spaces before or after the /dn? – terdon Jul 26 '17 at 13:16
  • 1
    All of these are very important details. Don't say "of course" when your question says that /dn is the "single word on one line". This is why it is important to show us representative examples of your data. – terdon Jul 26 '17 at 13:44

1. Using sed append

If you want a completely new line with the word /period after each line containing /dn then use sed append:

sed '\:\dn:a/period' filename

the output for your sample would be:


1. Notes

  • :\dn: search for \dn
  • a/period append /period to the next line (a new line).

2. Search and append to the end of next line

If you want the /period at the end of the next line then use it like this:

sed ':/dn: { N; s:$:/period: }' filename

Here is a sample input:


and the output:

/name /period

2. Notes

First we are searching for lines with /dn, then we add the /period at the end of ($) next line (N;).

  • The OP is very hard to understand, but I think they want the /period to be printed on the next line. – terdon Jul 26 '17 at 10:56
  • Updated the answer ;) – Ravexina Jul 26 '17 at 10:59
  • for the noobs, can you show full cmd code. using my expamle – Lunsy Jul 26 '17 at 14:16
  • @Lunsy updated the answer. – Ravexina Jul 26 '17 at 18:51

If file.txt contains string /dn then You can run this command:

$ grep -i "\/dn" file.txt | xargs -I{} printf "%s\n/period" {}

It will print

  • 4
    i) why are you making this case insensitive? That will also match Dn/, dN/ and DN/. The OP only specified dn/. ii) What's the point of the grep? That will print the entire line matching dn/, not the dn/ alone. More importantly, this will ignore all lines that don't have /dn, so change the OP's input. iii) you don't actually need the -I{} or {}, that's the default behavior of xargs. iv) you might want to add a \n after the /period as well. vi) You also don't need to escape the / for grep: grep /dn file.txt works fine. – terdon Jul 26 '17 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.