Need help with a sed script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help with a sed script
# 1  
Old 02-28-2012
Need help with a sed script

Hello,
I'm trying to write a sed script for this.
I have to delete the '\i' and '\b', except when it is between \* \*
Here is an example:

Before sed script:
Code:
I \* hope \bthat \b I \* \iwill \i find \*\bthe \b answer.\*

After sed script:
Code:
I hope \bthat \b I will  find \bthe \b answer.

What is the best way to do it?
Do I have to use pattern and hold space?
I'm looking for a general solution, that will work on every text.

Last edited by ikke008; 02-28-2012 at 03:21 PM..
# 2  
Old 02-28-2012
Not homework, I hope!

Not really a good sed problem. You can do it, but it seems messy. Only by counting can you tell that a \* is opening or closing a protected zone. If you split the line zones into lines you mark to support reassembly, like with a \ just before added end of line, then you can work on the safe lines and avoid the others, then reassemble. So, might be three sed's in a row! What do \*, \b and \i represent? 'i' does not need escape. Sometimes \b is backspace ^H. Are the \ literal in the input file?
# 3  
Old 02-28-2012
Not homework, it's from a specific markup language.
How can I do it by counting? Do I have to split the line then?
I thought when I use '\n' in a replacement, sed would see it as a character, not as a new line?
\* safe zone that cannot be changed /* can be changed /*can not be changed ....
# 4  
Old 02-28-2012
Hi ikke008,

Here it is. I struggled too much to get it. It must be homework because it has no sense to use sed with more suitable tools for the task. It may be wrong because it is a mess, but try:
Code:
$ cat infile
I \* hope \bthat \b I \* \iwill \i find \*\bthe \b answer.\*
$ cat script.sed
## Substitute '\*' with '\n'.
s/\\\*/\n/g

## If there is a odd number of '\*' or the string doesn't end with '\*', I 
## will append some at the end. There is no danger, but it will be used to
## avoid an infinite loop.
## 1.- Save content to 'hold space'.
## 2.- Remove all characters except '\n'.
## 3.- Remove one of them because next command will add another one.
## 4.- Put content in 'pattern space' to begin working with it.
## So, if in original string there were 3 '\*', now there will be 6. ˇFine!
h
s/[^\n]//g
s/\n//
H
g

## Label 'a'.
:a

## Save content to 'hold space'.
h

## Remove from first '\n' until end of line.
s/\(\n\).*$/\1/

## Remove all '\i' and '\b'.
s/\\b\|\\i//g

## Change '\n' with original '\*'.
s/\n//

## Append content to print as final output to 'hold space'.
H

## Recover rest of line from 'hold space'.
g

## Remove content printed just before.
s/[^\n]*//

## Save content to 'hold space'.
h

## Get first content between '\n'.
s/\(\n[^\n]*\n\).*$/\1/

## Remove all '\n' found.
s/\n//g

## Append content to print as final output to 'hold space'.
H

## Recover rest of line from 'hold space'.
g

## Remove content printed just before.
s/\n[^\n]*\n//

/^\n/ { 
    s/\n//g
    p   
    q   
}

ba
$ sed -nf script.sed infile
I  hope \bthat \b I  will  find \bthe \b answer.

Regards,
Birei
# 5  
Old 03-01-2012
Hello friends ,

I need help with passing array variabls in sed command on unix. I tried below command and it picks up the value from array but something is wrong as it throws garbled sed. Please help me fixing it.

sed -n "/(${tags[2]}\)/,/\(${tags[1]})\/p" patch_list.txt

Out put on unix is .....

sed: command garbled: /\(FW_12_10\)/,/\(FW_R12_06)\/p
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed script help

I am having a file as stated below : File 1: ########################## idnd a integer 2; list 1 ; list2 ; chip top alist( .a(1) , .b(2) , .c(3) , .d(1) , .e(7) , .n(80), .d(1) , .g(7) , .n(80), .f(1) , .e(7) , .m(80)); lis 7 nfj ; jdjd kn; jsjd l ; (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

sed script

I am beginner for Unix. I practicing unix shell script. I worked out some sed script example from internet. Everything fine. But in real unix environment, where sed script is mainly used.? Can anyone give some examples for the usage of sed script in real unix environment. It will be useful for... (1 Reply)
Discussion started by: gwgreen1
1 Replies

3. Homework & Coursework Questions

Sed script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a sed script that will display a list of all users in the /etc/passwd file that belong to the group... (0 Replies)
Discussion started by: lakers34kb
0 Replies

4. Shell Programming and Scripting

sed in script

I'm trying to write a simple script that replaces a string. The script works for uninterrupted strings, but as soon as sed encounters a space it stops reading the new string. I've tried double quotations in sed and backslashes before the $, however these don't work. Below is the script. Any help... (9 Replies)
Discussion started by: dsell002
9 Replies

5. UNIX for Dummies Questions & Answers

sed script

:rolleyes: I have a series of folders /temp/a /temp/b /temp/c In folders a, b, and c, I have files a1.txt..........a20.txt b1.txt..........b40.txt & c1.txt..........c60.txt Each file has the same data format :- Line... (2 Replies)
Discussion started by: grinder182533
2 Replies

6. Shell Programming and Scripting

sed script

how to convert the follow sed script file into a command line ? example: /^\.TS/,/^\.TE/{ /^$/p } I have tried the below but it is not working: # sed -n "/^\.TS/,/^\.TE/{/^$/p}" file file: 111 .TS 222 $333 << extract this line 444 .TE 555 (2 Replies)
Discussion started by: 3Gmobile
2 Replies

7. Shell Programming and Scripting

sed in a script

I am trying to run a sed command within a script to edit a file. I am trying to put the value of MYUSER into the sshd_config file. Instead of putting the value of the variable, MYUSER, it puts in the string ${MYUSER}. Anyone know a good solution to this? cat ${SSHD_CONFIG} | sed... (1 Reply)
Discussion started by: Mike_the_Man
1 Replies

8. Shell Programming and Scripting

Sed script

/\/\*/!b :x /\*\//!{ N bx } s/\/\*.*\*\/// This scipt should remove c like commnets /**/ i know what de last line does but i dont't know what the first lines do Can anyone explain please (3 Replies)
Discussion started by: clauchiorean
3 Replies

9. UNIX for Dummies Questions & Answers

help in sed script

I am having a shell script that connects to database half hourly and pulls out the backlog from some tables. Now that logfile is growing too big and I need to housekeep it! effectively I want to keep last 30 days data in that file and move rest to archived file. The file contents are as below.... (14 Replies)
Discussion started by: abhi123
14 Replies

10. Shell Programming and Scripting

Sed script maybe?

I have a lot of script files that were created by Extract in a dir that no longer exists. Now that I have to run these scripts they 'all' have to be changed. I'm looking for a way to do a 'mass' change if possible. So far, I've dumped all of the script file names to a file and sorted them to... (5 Replies)
Discussion started by: HOlli
5 Replies
Login or Register to Ask a Question