Find the answer to your Linux question:
Results 1 to 3 of 3
Hi I want to grep for a line and copy and paste that line. for Example ---- file abc.txt ---- host=atlx1 sid=atld1 mail=abc@abc.com host=atlx2 sid=atld2 mail=xyz@abc.com host=atlx3 sid=atld3 mail=def@abc.com host=atlx4 ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    1

    How to copy and paste line in shell script

    Hi

    I want to grep for a line and copy and paste that line.

    for Example

    ---- file abc.txt ----
    host=atlx1 sid=atld1 mail=abc@abc.com
    host=atlx2 sid=atld2 mail=xyz@abc.com
    host=atlx3 sid=atld3 mail=def@abc.com
    host=atlx4 sid=atld4 mail=mno@abc.com
    --- end of file abc.txt ----

    Now I want to grep line with host=atlx3 and sid=atld3 and copy and paste the line and ALSO put "#" in front of one of the line. The following is the output I would like to see...

    ---- file output.txt ----
    host=atlx1 sid=atld1 mail=abc@abc.com
    host=atlx2 sid=atld2 mail=xyz@abc.com
    #host=atlx3 sid=atld3 mail=def@abc.com
    host=atlx3 sid=atld3 mail=def@abc.com
    host=atlx4 sid=atld4 mail=mno@abc.com
    --- end of file output.txt ----

    How can I do this in shell script.

    Thanks in advance for your help.

    RK

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    awk>output.txt '/host=atlx3 sid=atld3/{print "#"$0}1' abc.txt

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Or
    Code:
    sed 's/host=atlx3 sid=atld3.*/#&/' abc.txt >output.txt
    Less to type, and you can use the -i flag and drop the output redirect to make the change straight to the input file.

    Interesting that neither of the solutions uses the asked-for grep, since that's an inappropriate tool for this problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...