sed: hold buffer question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: hold buffer question
# 1  
Old 03-14-2011
sed: hold buffer question

I've been using sed to help with reformatting some html content into latex slides using the beamer class. Since I'm new to sed, I've been reading a lot about it but I'm stuck on this one problem.

I have text that looks like this:

*******************

line of text that needs to be moved...

line of text that I want to keep.

line of text that I want to keep ... with a /latex_command{

************************Note: the above pattern repeats 500 times.

What I want to do is yank the first line and place at the end of the third line.

So far, I've invoked "sed -f command_file inputfile.tex >output_file.tex"

My command file begins like this:

/search string for file I want to move/h
/\/latex_command/x


I know this is wrong because it obliterates the third line altogether as it makes the switch wit the hold buffer. I can't find in the documentation the right way to append the first line to the third line without obliterating the third line all together. I also want to yank the first line so that it is removed completely.

I've tried the "x" command with an various search strings, but I end up
obliterating teh third line of text.

I fear my real problem is a vastly limited knowledge of the search syntax for sed. I mostly try to use what I know from vim.

I'm new to this forum. If I'm asking the wrong question in the wrong place or in the wrong way, just let me know. Any help would be appreciated. Thank you. [note: I only started to use sed because I couldn't find the right way to do this in vim. Maybe there is a way in vim to do this?]
# 2  
Old 03-14-2011
If I understand the requirement:
Code:
$ cat input
1111111
line2
line3
x
y
z
1111111
line2
line3
x
y
z
$
$ cat sedder
#! /usr/bin/ksh
exec < input
sed '/1111/{h;d;};/line3/{G;s/\n//;}'
exit 0
$
$ ./sedder
line2
line31111111
x
y
z
line2
line31111111
x
y
z
$

is probably pretty close.
# 3  
Old 03-14-2011
Assume your text is:

Code:
Move me1

Keep me1

Append to me1

Move me2

Keep me2

Append to me2

Move me3

Keep me3

Append to me3

Then a sed script like:

Code:
/^Move me/h
/^Move me/d
/^Append to me/b a
b
:a {
G
}

english:
For lines beginning with Move me, put them into the hold buffer.
For lines beginning with Move me, delete them (remember we have it in the hold buffer).
For lines beginning with Append to me, branch to label a
For all else, just default, print and read next
At label a, append the hold buffer to the pattern space... it then goes to default, print and read next


Would produce an output like:

Code:
Keep me1

Append to me1
Move me1


Keep me2

Append to me2
Move me2


Keep me3

Append to me3
Move me3

Am I getting close to what you were wanting??
# 4  
Old 03-14-2011
thanks

Thank you both Perderabo and cjcox. I can't get to my terminal now, but I will try both approaches. I'm sure I'll learn something with both. Thanks again and I'll report back with results.
# 5  
Old 03-14-2011
Quote:
Originally Posted by tfrei
So far, I've invoked "sed -f command_file inputfile.tex >output_file.tex"

My command file begins like this:
Code:
/search string for file I want to move/h
/\/latex_command/x

Your pretty close use h;d; to hold and delete original line and G to append:

Code:
/search string for file I want to move/{h;d;}
/\/latex_command/G

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed's hold-space to filter file contents

I wrote an awk script to filter "uninteresting" commands from my ~/.bash_history (I know about HISTIGNORE, but I don't want to exclude these commands from my current session's history, I just want to avoid persisting them across sessions). The history file can contain multi-line entries with... (6 Replies)
Discussion started by: ivanbrennan
6 Replies

2. Shell Programming and Scripting

Hold buffer in sed

Hi Experts, i have a file like below **** table name is xyz row count for previous day 10 row count for today 20 diff between previous and today 10 scan result PASSED **** table name is abc row count for previous day 90 row count for today 35 diff between previous and today 55... (4 Replies)
Discussion started by: Lakshman_Gupta
4 Replies

3. Shell Programming and Scripting

read is not on hold

In end of https://www.unix.com/shell-programming-scripting/103227-while-read-loop-scope-variables-shell.html mjd_tech gives script which can read some values directly without manually input, but if no value is the right one, my understand is, it will on hold for waiting the next input, but when I... (7 Replies)
Discussion started by: newoz
7 Replies

4. Shell Programming and Scripting

Hold, Replace and Print with sed

Hi, I'm a newbie with scripting so I'd appreciate any help. I have a file import.txt with below text AA_IDNo=IDNoHere AA_Name=NameHere AA_Address=AddressHere AA_Telephone=TelephoneHere AA_Sex=SexHere AA_Birthday=BirthdayHere What I need is that the Lines for Name, Address and... (3 Replies)
Discussion started by: heretolearn
3 Replies

5. Shell Programming and Scripting

Hold buffer in perl

Hi, Can any one tell me is their any "hold buffer" in perl similar to sed. I have to find a pattern, once that pattern found then need to go backward and find another pattern and print. Example: Below are the contents present in a file ## block IPs URLs URL_IPs Unblock URLs ... (4 Replies)
Discussion started by: Anjan1
4 Replies

6. Shell Programming and Scripting

sed pattern and hold space issues

Good day. Trying to make a sed script to take text file in a certain format and turn it into mostly formatted html. I'm 95% there but this last bit is hurting my head finally. Here's a portion of the text- Budgeting and Debt: Consumer Credit Counseling of Western PA CareerLink 112... (5 Replies)
Discussion started by: fiendracer
5 Replies

7. Shell Programming and Scripting

injecting new line in sed substitution (hold space)

Morning, people! I'd like to call upon your expertise again, this time for a sed endeavor. I've already searched around the forums, didn't find anything that helped yet. background: Solaris 9.x, it's a closed system and there are restrictions to what is portable to it. So let's assume I... (4 Replies)
Discussion started by: ProGrammar
4 Replies

8. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

9. Shell Programming and Scripting

Using sed buffer

Hi. I have some questions about using sed. I cannot use the hold buffer. For example i want to put first line to the buffer than take second line and append the buffer to the second line.then 3th to the all. It will be like 2->1->3 th lines. Any idea? (1 Reply)
Discussion started by: burakkilic
1 Replies

10. UNIX for Dummies Questions & Answers

buffer question

Hi... I have a question about how many lines the window can remember... is there an environemtn varible for this I can change??? I'm not talking about how many previous commands it remembers. I am talking about how many lines it remembers and for how many lines I can press "page-up" and I can... (4 Replies)
Discussion started by: lmanchur.
4 Replies
Login or Register to Ask a Question