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
# 8  
Old 05-15-2008
Hi era / Scripting experts

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

Thanks in advance
# 9  
Old 05-15-2008
It's not allowed to bump up questions! Please read the rules.
# 10  
Old 05-16-2008
It would be easier if the comment could go after the DISABLE line; is this an option?

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

to disable and

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

to enable again (assuming your sed understands \n).
# 11  
Old 05-16-2008
Code:
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
}'

# 12  
Old 05-16-2008
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.
# 13  
Old 05-16-2008
Code:
sed '/host=atlx3 sid=atld3 mail=DISABLE$/{;N;s/.*\n#//;}' abc.txt

# 14  
Old 05-16-2008
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....
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