Help, | ! & [ ^ in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help, | ! & [ ^ in sed
# 1  
Old 06-24-2009
Help, | ! & [ ^ in sed

Hi,everyone:
I'm new to shell, and I'm confued with some script:

Code:
ssum=`echo "$lsum" | sed 's|!|\\\\!|g'`
line1=`echo "$line" | sed 's!\[!\\\[!g' | sed 's!\]!\\\]!g'`
line3=`echo "$line2" | sed 's!\&!\\\&!g'`
sed "s^${line3}^${newline3}^g" ${TIER4FILE} > ${TMP_TIER4FILE}

In the first three lines, I'm not sure the "|","!","[" usage. I only know that "!" is used to inverse the action of a command. And is "s" here used to exchange strings?

In the last line, "s" is used as exchange command, but I don't understand the usage of "^".

SmilieCould anyone help me? Thanks?

Last edited by mycoy; 06-25-2009 at 05:28 AM..
# 2  
Old 06-24-2009
in sed the substitute command can be written in many syntax:-

example:

Code:
sed 's/old pattern/new pattern/g'    #  g means substitute globally.

sed 's!old pattern!new pattern!g'   # same result as above

sed 's^old pattern^new pattern^g'    # also same result as above

sed '_old pattern _new pattern_g'  # also same result

BR
# 3  
Old 06-24-2009
To ahmad.diab:
Thanks.

But in
Code:
sed 's|!|\\\\!|g'`

what does "|" mean?
# 4  
Old 06-24-2009
Quote:
Originally Posted by mycoy
To ahmad.diab:
Thanks.

But in
Code:
sed 's|!|\\\\!|g'`

what does "|" mean?
because ! is a special char. you need to escape it by "\" if you want to search for it ... if you didn't scape it the system will see it as a negate (not) char .
BR
# 5  
Old 06-24-2009
A substitution requires two parts: the part you want to replace; and the part you want to replace it with. These parts are separated by a character that you chose. So after the "s" you say what the delimiting character should be:

Code:
s|original_string|new_string|

would use a | as the delimeter. This could be almost any character:

Most people use a /, but for example if the expression you want to replace, or the one you want to replace it with contains slashes, then it's convenient to use something else:

Code:
s+original_string+new_string+
sXoriginal_stringXnew_stringX

It's all the same thing.
# 6  
Old 06-24-2009
Thanks all.

---------- Post updated at 08:36 PM ---------- Previous update was at 09:01 AM ----------

Last edited by mycoy; 06-25-2009 at 06:46 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo "abc" | sed - r 's/a/&_&/

I want to know the working of & here step by step using sed command. (1 Reply)
Discussion started by: Preeti07
1 Replies

2. UNIX for Beginners Questions & Answers

sed pattern &

Hi, I can't seem to understand what the sed and & do.. Why is the set of numbers appear twice ? how the command really work ? echo "123 abc" | sed 's/*/& www &/' Output: 123 www 123 abc (3 Replies)
Discussion started by: uniran
3 Replies

3. Shell Programming and Scripting

Help with sed ( & )

I am searching for a specific pattern and replacing with ( ) for matched pattern .I am not getting the expected result ..... Here is the file cat test cool change Frinto Francis Frinto cool change Attitude /usr/bin sed 's/*/( & )/' test ( cool ) ( change ) ( )Frinto Francis... (2 Replies)
Discussion started by: frintocf
2 Replies

4. UNIX for Dummies Questions & Answers

awk & sed

Hi, Can anyone let me know the difference between awk and sed utilities in Unix? Many thanks. (2 Replies)
Discussion started by: venkatesht
2 Replies

5. Shell Programming and Scripting

sed & areas respectively sed & pyramiding

Hello everyone, i wonder if someone could give me an advice regarding the following problem using sed. Given ist a structure as shown below: <aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa> Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags... (4 Replies)
Discussion started by: Donaldinho
4 Replies

6. Shell Programming and Scripting

sed & awk

Hi. I'm going to learn scripting and i have the following topics on the list: sed, awk, shell scripting, perl. My question is, whehter i should learn sed and awk? Aren't this tools outdated? Although i see that GNU upgrade it's versions of these tools from time to time. And, the next... (9 Replies)
Discussion started by: kukuruku
9 Replies

7. Shell Programming and Scripting

New to Sed & Awk

How do I grab the first 10 characters of a line and append it to another empty file? (7 Replies)
Discussion started by: xgringo
7 Replies

8. Shell Programming and Scripting

sed, date and /&

Hi, $ echo 1 titi | sed -e "s/1/$(echo \&)/" 1 titi but $ echo 1 titi | sed -e "s/1/$(date \&)/" date: invalid date `&' titi how can i do for handle '\&' with date ? Thx (7 Replies)
Discussion started by: sncr24
7 Replies

9. Shell Programming and Scripting

cp & sed error

Hi, Below is a small piece of my Korn shell script - what i am trying to do is substitute all occurrences of the word given by the ${src} parameter with the word given by the ${dest} parameter in that particular textfile. But i get the errors below... for i in `ls... (19 Replies)
Discussion started by: n8575
19 Replies

10. Shell Programming and Scripting

sed & awk help...

I have a question. Take the following statement awk -F\| '{print $21}' testfile | sed 's/\//\\/g' > newfile This will grab the 21st column of a | delimited text file, replace the forward slashes "/" , with back slashes "\", and redirect the output newfile. Now, how do I get the output... (4 Replies)
Discussion started by: shimb0
4 Replies
Login or Register to Ask a Question