SED removing CR/LF


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED removing CR/LF
# 1  
Old 11-05-2010
SED removing CR/LF

I have a source text file that contains something like this:
Code:
<start_template>
bool function( void *<var_name> )
{
        // Blah Blah
}
</start_template>

Following is used to remove this template from source file.
Code:
template=$(sed -n -e '/<start_template>/,/<\/start_template>/{
/<start_template>/b
/<\/start_template>/b
p
}' template.tpl )

I can then display the template in all its true glory with crlf.
Code:
echo "$template"

bool function( void *<var_name> )
 {
         // Blah Blah
 }

Now I want to replace <var_name> with 'hello'
Code:
template=$(echo $template | sed -e "s/<var_name>/hello/g")

Works but all the CRLF's seem to have been removed!
Code:
echo "$template"
bool function( void *hello ){// Blah Blah}

Ok, so lets not do the replace, but just write the un modified template to another file
after a particular line that contains say 'insert after this line'
Code:
sed -i -e "/insert after this line/a\ `echo $template`" target_file.txt

Checking target_file.txt, the template has been inserted in the correct place but again cr/lf have been removed.
Code:
 'insert after this line'
bool function( void *hello ){// Blah Blah}

I'm kind of using this to learn sed as well as write a small utility for my one use. I hate giving up and I'm sure it can be done.

Anyone have any idea how I can keep cr/lf when I do the replace and the write
to the new file?

thanks in advance.

Last edited by Franklin52; 11-05-2010 at 01:05 PM.. Reason: Please use code tags
# 2  
Old 11-05-2010
It's the shell that's doing it, not sed. Put quotes around it, i.e. template="$(stuff)" instead of template=$(stuff)

Instead of storing it to a variable and printing it back, why not let sed print to stdout instead? Otherwise you may at some inconvenient point discover the limitations on lengths of a shell variable on your system.
# 3  
Old 11-05-2010
Need quotes around the template var to stop shell removing all white space, see change in red below:

Code:
template=$(echo "$template" | sed -e "s/<var_name>/hello/g")

# 4  
Old 11-05-2010
Wow, thanks for that. Will give it a go tomorrow and let you both know how it goes.

Thanks for such a quick response.

---------- Post updated 11-05-10 at 12:32 PM ---------- Previous update was 11-04-10 at 11:20 PM ----------

Thanks all, template="$(stuff)" instead of template=$(stuff) works a treat for the replace however I'm still having issues with writing out template to new file after text 'insert after this line'

sed -i -e "/insert after this line/a\ `echo $template`" target_file.txt

Replaces crlf again, any ideas? I'm guessing its the placement of quotes again but having no luck finding where the issue is.

tks
# 5  
Old 11-05-2010
Another way to replace the function name is:
Code:
awk '/<start_template>/{f=1}
f && /bool function/{sub("<var_name>","hello");f=0}
' sourcefile > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing unwanted symbols with sed

I would like produce blue, green, red, yellowfrom"blue:,*green:,*red:,*yellowI can remove the colon with echo "blue:,*green:,*red:,*yellow" | sed 's/://g'which givesblue,*green,*red,*yellowbut when I try echo "blue:,*green:,*red:,*yellow" | sed 's/://g'; 's/*//g'I get bash: s/*//g: No such... (9 Replies)
Discussion started by: Xubuntu56
9 Replies

2. Shell Programming and Scripting

Removing quotes in sed

I have a file and I want to remove quotes from the word " ` " through sed command but unable to remove I am using below command sed s/"`XYZ`"/"ZXY"/g file1.txt > file2.txt But this is not working. How can we remove "`" through sed command (2 Replies)
Discussion started by: kaushik02018
2 Replies

3. Shell Programming and Scripting

removing html format with sed

Hello i am trying to remove the html format from the file using sed. for example remove <p> </p> i tried to do this : sed -e 's/<*>//g' test > test.t but still i have some html format . please help if you have any suggestions lets say this is the html file 1... (11 Replies)
Discussion started by: koricha
11 Replies

4. Shell Programming and Scripting

sed removing values

Is there an easy way in sed to remove a character and values after that character? ex: blackout_10-11-2011(NODE_LEVEL) I want to remove '(' and everything after. so I will only have. blackout_10-11-2011 I can use a cut command to do this but interested in seeing how its done... (1 Reply)
Discussion started by: BeefStu
1 Replies

5. UNIX for Dummies Questions & Answers

Removing end of line using SED

Hello Friends, How can I remove the last two values of this line using sed John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638:9/3/90:45900 The result should look like this: John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638 (3 Replies)
Discussion started by: humkhn
3 Replies

6. Shell Programming and Scripting

Removing domain suffix with SED

Hi Experts, I have a syslog file from 1000's of different hosts which I want to adjust by removing the domain suffix from the hosts. My previous attempts haven't managed to match all the different lenghts of the subdomains which are being logged. Could somebody suggest which sed syntax... (6 Replies)
Discussion started by: krypton
6 Replies

7. Shell Programming and Scripting

Removing a character using sed

Hi, I have a file with the text below. How do i remove the character "%" from the text file using sed ? Can anybody help ? 0% 68% 72% 0% 54% 33% 75% 24% 6% 59% 77% 77% 33% (6 Replies)
Discussion started by: Raynon
6 Replies

8. Shell Programming and Scripting

Removing lines with sed

Here is some code output that I have: architecture ppc cputype CPU_TYPE_POWERPC cpusubtype CPU_SUBTYPE_POWERPC_ALL offset 4096 size 184464 align 2^12 (4096) architecture ppc64 cputype CPU_TYPE_POWERPC64 cpusubtype CPU_SUBTYPE_POWERPC_ALL offset 192512 ... (5 Replies)
Discussion started by: pcwiz
5 Replies

9. Shell Programming and Scripting

Removing '." character using SED

HI All, Have some files which contains some string like, "create .<table1> as" "insert into .<table2> values", i want to replace ".<table1>" with only "<table1>", i.e removing '.' character in ksh, i have written below code but it is not removing the dot character, any help? for name... (2 Replies)
Discussion started by: arvindcgi
2 Replies

10. UNIX for Dummies Questions & Answers

sed: removing backticks from certain lines

Hi, I would like to change some lines in my mysql-dump, because there a syntax problems with some version of mysql. I 'd like to change USE ´someDatabase´; to USE someDatabase; (without backticks) using the sed command in the shell Thanks & best regards Bernd (5 Replies)
Discussion started by: bjb
5 Replies
Login or Register to Ask a Question