How to copy and paste line in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy and paste line in shell script
# 1  
Old 05-14-2008
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  
Old 05-14-2008
Assuming this precise sequence of host and sid is the deciding criterion, maybe something like

Code:
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.
# 3  
Old 05-14-2008
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
# 4  
Old 05-14-2008
Code:
sed '/host=atlx3 sid=atld3/{;h;s/^/#/p;x;s/mail=.*/mail=DISABLE/;}' abc.txt

# 5  
Old 05-14-2008
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
--------------------
# 6  
Old 05-14-2008
Assuming the # at beginning of line is always a sign that this should be done,

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

# 7  
Old 05-14-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy n paste n times

I have one mainframe copy book where I want to copy n times depend on occurs which mention below. Example: Below highlighted row mention “occurs 2 times” so I need to copy 2 times till next label 10. C14992 10 FILLER PIC X(2835). 01 ... (7 Replies)
Discussion started by: srivalli
7 Replies

2. Shell Programming and Scripting

Paste command not working in shell script

Hai , When i use paste command in command prompt its giving expected output but not in the script. Below is the example. $cat file 1 2 3 $cat file1 4 5 6 $paste -d ':' file file1 1:4 2:5 3:6 but when i used the same command in script its giving the output as below : 1 2 3 (3 Replies)
Discussion started by: Subbu123
3 Replies

3. Shell Programming and Scripting

Copy some line and paste it after some line in same file

Hi, I want to know how I can copy line 6th to 10th and paste it after 17th line in same file. Thanks, Biplab (19 Replies)
Discussion started by: Biplab
19 Replies

4. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

5. Shell Programming and Scripting

Copy and Paste to a new document

Hello, I am quite new to shell scripting so don't know all the tools available. What I'm trying to do is open a file optimal.txt search for objectiveValue and copy the number in quotes next to it. e.g. ... solutionName="incumbent" solutionIndex="-1" objectiveValue="13246" ... (6 Replies)
Discussion started by: StephanR
6 Replies

6. UNIX for Dummies Questions & Answers

Copy/Paste in Vi editor

Dear All, I have a file containing 12 lines. First 3 lines have 9 values and the remaining 9 lines with no values. I was trying to copy and paste these 9 values of the first 3 lines into last 9 lines simultaneously as A=1.491331, B=1.539000 ..... but I don't know how to cope with this... (9 Replies)
Discussion started by: sullah
9 Replies

7. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

8. Shell Programming and Scripting

copy/paste with awk

Hi everybody, I have two XML files. I am working on a script that could copy and paste the contents of the first xml file to the desired location in the second xml file. Here is my first XML file. This is the second XML file. Finaly, I wnat to obtain something like that : ... (2 Replies)
Discussion started by: lsaas
2 Replies

9. Shell Programming and Scripting

copy and paste a specific line

Hi I am having some trouble cut and paste a file based on the content of another file. I have a file called draft. I need to cut and paste its content to another file based on the content of a file called proc.txt The content of proc.txt is like the following:... (7 Replies)
Discussion started by: tiger99
7 Replies
Login or Register to Ask a Question