Explain following sed syntax please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explain following sed syntax please
# 1  
Old 04-17-2010
Explain following sed syntax please

Thanks to this forum I have managed to work out a solution to my problem and actually understand most of it, but one thing is confusing me and I am sure someone here can explain.

I need to insert a piece of txt into a file. This txt is

awk '{ sub(/$/,"\r"); print }' $JCL_WBB50103_EFTOUT > $JCL_WBB50103_EFTOUT".crlf"

I wrote the following test script

Code:
BLOB="awk '{ sub(/$/,\"\\\r\"); print }' \$JCL_WBB50103_EFTOUT > \$JCL_WBB50103_EFTOUT\".crlf\""
export BLOB


sed "/jcl_ikjeft01 WBB50103/a\\
        $BLOB" /home/iis1/EFTONCR > /home/iis1/tmp

The bit I don't understand is why I need 2 (\\) before the \r statement. I would have thought 1 would have done the trick

Thanks in advance

Chris
# 2  
Old 04-17-2010
From sed

Quote:
[1addr]a\
text
Write text to standard output as described previously.
Quote:
The argument text shall consist of one or more lines. Each embedded <newline> in the text shall be preceded by a <backslash>. Other <backslash> characters in text shall be removed, and the following character shall be treated literally.
After the backslash is processed, according to that rule:
\r becomes r
\\r becomes \r
... so sed needs to see \\r to get the \r through.

Within the shell double-quoted string used to assign to BLOB, \\r would become \r, so you need to escape the first backslash with another backslash, \\\r, so that \\r appears when $BLOB is expanded in the sed command.

Regards,
Alister

Regards,
Alister

Last edited by alister; 04-17-2010 at 12:50 PM..
# 3  
Old 04-17-2010
Alister

Thanks for the prompt reply and concise explanation. That now makes sense. Am relatively new to shell scripting and have avoided sed, but decided to persevere in this case and very pleased I did

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Explain sed command

sed -e 's,*$,,' Can someone explain the options in this command? (2 Replies)
Discussion started by: scj2012
2 Replies

2. UNIX for Dummies Questions & Answers

Can someone please explain sed -n '/^$/!{s/<[^>]*>//g;p;}' filename

I came across this sed expression, and it does exactly what I want. However I haven't got the faintest clue how it does it and thus do not feel capable of using it. Can someone please explain how this expression works? (I used it to remove html tags in a html file I was converting to text) ... (3 Replies)
Discussion started by: maximus73
3 Replies

3. Shell Programming and Scripting

can you explain this sed code?

can anyone please explain this code? sed ':a;N;$!ba;s/]\n//g' file it replaces lines ending with "]" and concatenates with the next line so that line1] line2 becomes line1line2 i don't understand this part: :a;N;$!ba; I have noted that I can replace "a" with any letter: ... (1 Reply)
Discussion started by: locoroco
1 Replies

4. UNIX for Advanced & Expert Users

Please explain this sed one liner

Can anyone explain the below sed oneliner? sed -e ':a' -e '$q;N;11,$D;ba' It works same as tail command. I just want to know how it works. Thanks ---------- Post updated at 11:42 PM ---------- Previous update was at 11:37 PM ---------- Moderators, Can you please delete this thread?... (0 Replies)
Discussion started by: pandeesh
0 Replies

5. Shell Programming and Scripting

Please explain this SED expression

can anyone please explain this code? sed ':t /<VirtualHost/,/VirtualHost>/ { /VirtualHost>/!{ $!{ N; bt } }; /name/d; }' infile (4 Replies)
Discussion started by: jacky29
4 Replies

6. Shell Programming and Scripting

sed s/// syntax help

<tr><td width=10% style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>Package</b></td><td width=10% valign=center style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>JTs</b></td> This is got to be simple. I run this on the above .html file: sed... (8 Replies)
Discussion started by: dba_frog
8 Replies

7. Shell Programming and Scripting

sed -e 's%/$%%' explain this command

mount -ps | tail -1 | awk '{print $1}' | sed -e 's%/$%%' can you please explain this command mainly sed -e 's%/$%%' this.... (5 Replies)
Discussion started by: rsivasan
5 Replies

8. Shell Programming and Scripting

Explain SED code

Hi, Can anyone pls explain me the below SED code in detail. sed -e :a -e '$!N;s/\n//;ta' -e P -e D When this code is executed with a file(has 1lac records), it is taking very long time to process. So I wanted to modify this SED code with equivalant AWK code. Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

9. UNIX for Advanced & Expert Users

perl explain syntax !!!

hi all i was going through some perl code i came across this line and i am not getting what is exactly going on .. $$this{localtion} = GetName->GetVarName("EXE_DIR") ; what is the red part doing in above code (2 Replies)
Discussion started by: zedex
2 Replies

10. Shell Programming and Scripting

please explain this sed shell script to remove C++ comments.

#! /bin/sed -nf # Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com) # Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org) # Works its way through the line, copying to hold space the text up to the # first special character (/, ", '). The original... (1 Reply)
Discussion started by: Priyaranjan
1 Replies
Login or Register to Ask a Question