1827474 Members
1760 Online
109965 Solutions
New Discussion

Re: sed-append question

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

sed-append question

I have the following input

load /BC/TOC1
load /BC/TOC2

I want to add one more load after the last one, in this case: "load BC/TOC2"

sed "/^load/a\load /BC/TOC3" $file

it runs the sed twice :-( like the following
load /BC/TOC1
load /BC/TOC3
load /BC/TOC2
load /BC/TOC3


how do I change the sed statement to only append to the last one?

the desired output will be
load /BC/TOC1
load /BC/TOC2
load /BC/TOC3

thank you
16 REPLIES 16
Abhijeet_3
Frequent Advisor

Re: sed-append question

Alternative way is
echo " load/BC/TOC3" >> filename

Abhijeet
Gemini_2
Regular Advisor

Re: sed-append question

it will not work because I have other stuff in the file. I just show you guys the portion of the file that I want to modify.
James R. Ferguson
Acclaimed Contributor

Re: sed-append question

Hi Gemini:

Try:

# cat ./append
#!/usr/bin/sed -f
n
/^load TOC2/p
a\
load \/BC\/TOC3

...run as:

# ./append file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: sed-append question

Hi (again):

Sorry, that should look like:

# cat ./append
#!/usr/bin/sed -f

#!/usr/bin/sed -f
/^load \/BC\/TOC2/{
a\
load \/BC\/TOC3
}

...run as:

# ./append file

Regards!

...JRF...
Gemini_2
Regular Advisor

Re: sed-append question

sorry, I am still not making myself clear.

the last entry may not always be /BC/TOC2, it can be /BC/TOC3, /BC/TOC4, the whole point is how to append the new entry just ONCE?

thank you
James R. Ferguson
Acclaimed Contributor

Re: sed-append question

Hi Gemini:

# cat ./append
#!/usr/bin/sed -f
${
a\
load \/BC\/TOC_
}

# ./append file

...will append to the file's end.

Regards!

...JRF...

Gemini_2
Regular Advisor

Re: sed-append question

are you sure?

is the difference between yours and mine the "_"? I still get the same result :-(

please help out...
James R. Ferguson
Acclaimed Contributor

Re: sed-append question

Hi:

Well, mine works for me, so try this variation:

# #!/usr/bin/sed -nf
p
${
a\
load \/BC\/TOC_
}

...substitute anything you want for "TOC_".

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: sed-append question

I don't think you can use sed at all. Also, do you have more than one sequence of loads that can be present? If so, you can't even use awk unless you make a grep -n prepass:

#!/usr/bin/ksh
FILE=itrc_append

# fine last "load"
line=$(grep -n "^load" $FILE | tail -1 | sed 's/:.*$//')

# add one "load /BC/TOC3" after the last "load".
sed -e "${line}a\\
load /BC/TOC3" $FILE
Gemini_2
Regular Advisor

Re: sed-append question

okay....

I should give a complete file to make my point clear

the file is like

----------------------
load /BC/TOC1
load /BC/TOC2

description: testing
vob: bc
---------------------

so, I can not just append at the end of the file..I need to append at the end of the load statement..

the solutions above are all at the end of the file.....





Dennis Handly
Acclaimed Contributor

Re: sed-append question

>the solutions above are all at the end of the file.....

Mine is not. I find the LAST "load" and add after it.
Sandman!
Honored Contributor

Re: sed-append question

Try the sed command below. It inserts the line "load /BC/TOC3" right after the last load line in the input file.

sed '
/^load/,/^$/{
/^$/{i\
load /BC/TOC3
}
}' infile

~hope it helps
Dennis Handly
Acclaimed Contributor

Re: sed-append question

Sandman's seems to work only if there is a blank line after the load. Mine works no matter how many blocks of "load"s or what follows them.
Gemini_2
Regular Advisor

Re: sed-append question

Thanks for Dennis Handly's solution. that works very well.

I also like Sandman's solution, but I have question about your synatx

sed '
/^load/,/^$/{
/^$/{i\
load /BC/TOC3
}
}' infile

1.what does the first/second line mean?

I am confused at
/^load/,/^$/{
/^$/{i\

that is a new synatax to me..

please kindly shed some light..
Dennis Handly
Acclaimed Contributor

Re: sed-append question

>1.what does the first/second line mean?

Basically for all lines from the first "load" to a blank line, do the {} block. Inside the block it tests to make sure it is on the blank line and if so, insert.
Sandman!
Honored Contributor

Re: sed-append question

My sed solution was based on the input file you had provided i.e

----------------------
load /BC/TOC1
load /BC/TOC2

description: testing
vob: bc
----------------------

>I am confused at
>/^load/,/^$/{
>/^$/{i\

...it's a nested sed construct which applies commands at a particular address which is within the selected block. So the above command grabs all lines that start with the word load (^load) upto the first blank line (^$). Once it reaches the blank line it inserts the desired line above the blank one.

hope it helps!