Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed command to replace a string!!!! 1

Status
Not open for further replies.

maryam421

MIS
Apr 5, 2004
102
US
Hi,
I am trying to change a vlue in a line based on position. Like if I have line that 11231234, I want to change position 4 to 7 by abc. I have am using sed command but don't know how to do it by position. sed does the pattern matching based on string but I could not find a way to search it via position. I did get sample code from net but it just replaces the position. Following is the code:

echo 1235678 |sed 's/^\(1.\{1\}\)./\1/'

Please give any sugestions to replace the string or any options with sed command that I can use.
 
Code:
 echo '123456789' | sed 's/^\(...\)\(...\)/\1abc/'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
or
Code:
 echo '123456789' | sed 's/^\(.\{3\}\)\(...\)/\1abc/'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Can you please explain what you did? It does work. Can i use a varible to change the value like
cat <file_name> | sed 's/^\(.\{3\}\)\(<old_varible>)\)/\1<new_variable>/'

Or
can you please tell me how to change the a string based on position?
 
Code:
sed "s/^\(.\{3\}\)\(${oldVariable})\)/\1${newVariable}/"

from the beginning of a line [^], any 3 character [.\{3\}] <-- capture #1, followed by the value of a variable oldVariable <-- capture #2

replace the above with:
capture #2 followed by the value of a variable newVariable.

Futhermore doing 'man regexp' yields:
Code:
     4.1       A circumflex (^) at the beginning of an entire  RE
               constrains  that RE to match an initial segment of
               a line.

     2.3       A one-character RE followed by \{m\},  \{m,\},  or
               \{m,n\}   is   a   RE  that  matches  a  range  of
               occurrences of the one-character RE. The values of
               m  and  n  must be non-negative integers less than
               256; \{m\} matches exactly m  occurrences;  \{m,\}
               matches  at  least  m occurrences; \{m,n\} matches
               any  number  of  occurrences  between  m   and   n
               inclusive.   Whenever  a  choice  exists,  the  RE
               matches as many occurrences as possible.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
true - don't need one:
Code:
sed "s/^\(.\{3\}\)${oldVariable}/\1${newVariable}/"

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for your reply. Command works great. But when I put the varible it takes it as value rather than evalute the string.
cat <file name>|sed 's/^\(.\{44\}\)\(......\)/\1$x/'
The result show as $x rather than the value. Please help.
Thanks
 
Code:
sed [COLOR=red]"[/color]s/^\(.\{44\}\)\(......\)/\1$x/[COLOR=red]"[/color] [COLOR=green]fileName[/color]

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You rock!!!!. Thanks for all your help. I am shining infornt of my boss.
Keep up the good work!!!
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top