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

Assuming this precise sequence of host and sid is the deciding criterion, maybe something like

sed '/host=atlx3 sid=atld3/{;h;s/^/#/p;x;}' abc.txt

This takes a matching line and remembers it in the hold space (h); then substitutes a hash mark at beginning of line and prints the result; then swaps back the hold space (x) which will be printed as any other line a second time.

Thank you so much for your help.

Another question :

How can I also replace "mail=def@abc.com" with "mail=DISABLE" in the line which is NOT commented out ('#').

Thanks Again

sed '/host=atlx3 sid=atld3/{;h;s/^/#/p;x;s/mail=.*/mail=DISABLE/;}' abc.txt

Thank You, works perfectly....

Sincere apologies for asking another question but I am very new to scripting and need some HELP.

Question:

How to reverse it.. means find the line with host=atlx3 sid=atld3 and mail=DISABLE and then delete this line and at the same time un-comment ('#') the line above it..

--- INPUT.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=DISABLE
host=atlx4 sid=atld4 mail=mno@abc.com
-------------------------

---- 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=atlx4 sid=atld4 mail=mno@abc.com
--------------------

Assuming the # at beginning of line is always a sign that this should be done,

sed -e '/^#/{;s///p;N;d;}' INPUT.TXT

I wanted to look for host and sid and then delete a line with mail=DISABLE and un-comment the line above it. The reason... in actual there may be many lines in the file with '#' at the beginning but wanted to delete as per the above criteria and then un-comment the line above it.

thanks again

Hi era / Scripting experts

I still help to know how to reverse the process..

Thanks in advance

It's not allowed to bump up questions! Please read the rules.

It would be easier if the comment could go after the DISABLE line; is this an option?

sed '/host=atlx3 sid=atld3/{;h;s/mail=.*/mail=DISABLE/p;x;s/^/#/;}' abc.txt

to disable and

sed '/mail=DISABLE$/{;N;s/.*\n#//;}' abc.txt

to enable again (assuming your sed understands \n).

cat filename | sed '/host=atlx3 sid=atld3/p' | awk 'BEGIN{n=0}
{
if (index($0,"host=atlx3 sid=atld3")!=0 && n==0)
{
        print "#"$0
	n=1
}
else
	print
}'

Yes, the comment could go after the DISABLE line but in the second part of the sed command if there are multiple entries with comment '#' and DISABLE THEN I want to un-comment and delete the DISABLE line based on certain host and sid.

for example:
**********
--- start of 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 abc.txt --------

Then I issue the command

sed '/host=atlx3 sid=atld3/{;h;s/mail=.*/mail=DISABLE/p;x;s/^/#/;}' abc.txt

then I issue the command for another host and sid

sed '/host=atlx4 sid=atld4/{;h;s/mail=.*/mail=DISABLE/p;x;s/^/#/;}' abc.txt

after issuing the above two commands, here's the NEW abc.txt file

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

NOW I want to uncomment the entry for host=atlx3 sid=atld3 and remove the line with host=atlx3 sid=atld3 and mail=DISABLE.

Hope I am clear with my requirement.

Again THANK YOU so much for your help.

sed '/host=atlx3 sid=atld3 mail=DISABLE$/{;N;s/.*\n#//;}' abc.txt

Thanks era, it works but there are still some issues.

if there's more than one blank space between host and sid then it does not work AND also if there are more than one line with the same host and sid it comments '#' all lines having the given host, sid.

for example
*********

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

by running the command ....

sed '/host=atlx3 sid=atld3/{;h;s/mail=.*/mail=DISABLE/p;x;s/^/#/;}' abc.txt

it gives the following output...

host=atlx1 sid=atld1 mail=abc@abc.com
host=atlx2 sid=atld2 mail=xyz@abc.com
#host=atlx3 sid=atld3 purpose=development
host=atlx3 sid=atld3 mail=DISABLE
#host=atlx3 sid=atld3 mail=def@abc.com
host=atlx4 sid=atld4 mail=mno@abc.com

it should not comment '#' the line shown in red above (with purpose=development)

Again Thank You so much for your help, without your help I would still be reading the man pages....

sed '/host=atlx3  *sid=atld3 mail=/{;h;s/mail=.*/mail=DISABLE/p;x;s/^/#/;}' abc.txt

There are two spaces and an asterisk between host and sid now. space asterisk means zero or more spaces, so makes multiple spaces optional.

AU Main Date: 08-SEP-08 10:27:11
Profit and Loss Long Report Page: 1
Current Period: AUG-08

Personal Unit=AU003 (Industrial Products Division),Plant=B00089,Departmant=D110
Ptd-Actual Ptd-Budget Ptd-Variance YTD-Actual YTD-Budget YTD-Variance
Account Le BU Pl De In Fu
------------------------------ -- -- -- -- -- -- ------------ ------------ ------------ -------------- ------------- ------------
Operating Distribution 0.00 0.00 0.00 -30,808,210.75 0.00 30808210.75

      Cost of Sales/OPNS                        180,689.57           0.00     -180,689.57   173818430.54            0.00  -173818430.54

      GROSS PROFIT                              180,689.57           0.00     -180,689.57   -118521808.49           0.00  118521808.49 

           Marketing Distrib                          0.00           0.00           0.00            0.00            0.00          0.00 

Personal Unit=AU004 (Industrial Products Division),Plant=B01189,Departmant=D111

                                             Ptd-Actual     Ptd-Budget      Ptd-Variance    YTD-Actual       YTD-Budget   YTD-Variance                                          

Account Le BU Pl De In Fu
------------------------------ -- -- -- -- -- -- ------------ ------------ ------------ -------------- ------------- ------------
Operating Distribution 0.00 0.00 0.00 -30,808,210.75 0.00 30808210.75

      Cost of Sales/OPNS                        180,689.57           0.00     -180,689.57   173818430.54            0.00  -173818430.54

      GROSS PROFIT                              180,689.57           0.00     -180,689.57   -118521808.49           0.00  118521808.49 

           Marketing Distrib                          0.00           0.00           0.00            0.00            0.00          0.00 

I need oput where ever i find Personal unit just capture that line and copy that line to make it like

Personal Unit=AU003
Plant=B00089
Departmant=D110
-->RAM:AU003:B00089:D110:08-SEP-08:US Primary

I am using

sed -e '/Personal Unit/{;h;s/^/#/p;x;}' -e '/Business Unit/{s/,Plant/\nPlant/g' -e 's/,Departmant/\nDepartmant/g'} filename

But this is not fulfilling my requirement
Any one please help me on this

This is a duplicate of this post:

http://www.unix.com/shell-programming-scripting/83485-grep-then-format-then-prepare-string.html\#post302242263

No duplicate or cross-posting, please read the:

Simple rules of the UNIX.COM forums:.

Regards