Dharak Shah
Dharak Shah

Reputation: 11

copy a line with specific pattern and paste it below in unix without opening a file

I have a specific requirement.

I have file:

some text

 . . . . .

 . . . . .

**todo: owner comments . . . .** 

... .

sometext

Now I want the output like:

some text

 . . . . .

 . . . . .

**todo: owner comments . . . .** 

**owner: todo comments . . . .** 

... .
   .

sometext

I want to grep for todo and copy that line and paste it below with above modification. Can it be possible without opening a file... like sed,awk command ??

Thanks and Regards, Dharak

Upvotes: 0

Views: 499

Answers (2)

Sadhun
Sadhun

Reputation: 264

sed 's/\(**todo: owner comments\)\(.*\)/\1 \2  \
>   **owner: todo comments \2/g' filename
  1. Patterns matched and replaced
  2. New line is inserted manually by placing an enter after '\' at end of first line
  3. '>' will come automatically pointing for the next characters to be inserted

Upvotes: 0

karakfa
karakfa

Reputation: 67467

I guess what you mean is opening the file in an editor. Here is an awk script you can tailor for your needs.

$ awk '/\*\*todo:/{print; print "**owner: todo ... ";next}1' file 
some text

 . . . . .

 . . . . .

**todo: owner comments . . . .** 
**owner: todo ... 

... .

sometext

you can save the output to a temp file and move over to your original file.

Upvotes: 1

Related Questions