How to use sed to insert character in the beginning of file path?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to use sed to insert character in the beginning of file path?
# 1  
Old 09-27-2019
How to use sed to insert character in the beginning of file path?

I need to manipulate one Database file on Solaris 11 in which contains more than 5000 lines of data file path like this:

Code:
 '/data1/oradata/DBNAME/system01.dbf',
  '/data7/oradata/DBNAME/undotbs1_01.dbf',
  '/data1/oradata/DBNAME/sysaux01.dbf',
  '/data28/oradata/DBNAME/userdata01.dbf',
  '/data28/oradata/DBNAME/user_data01.dbf',
  '/data128/oradata/DBNAME/undotbs1_02.dbf',
................................................
  '/datadw29/oradata/DBNAME/corp/comm_sd37.dbf',
  '/datadw28/oradata/DBNAME/corp/comm_sd38.dbf',
.................................................................................................
  '/dataidxdw42/oradata/DBNAME/ods/ODS_DATA_IDX_07.dbf',
  '/dataidxdw42/oradata/DBNAME/ods/ODS_DATA_IDX_08.dbf'

,

In these path lines, the first part of path name contains number '/data28/ and three path group with different first part of name. I need to insert one path name: "bck1" in the beginning of each path name. After manipulation, the path name in the file should be like this:

Code:
 '/bck1/data1/oradata/DBNAME/system01.dbf',
  '/bck1/data7/oradata/DBNAME/undotbs1_01.dbf',
  '/bck1/data1/oradata/DBNAME/sysaux01.dbf',
  '/bck1/data28/oradata/DBNAME/userdata01.dbf',
  '/bck1/data28/oradata/DBNAME/user_data01.dbf',
  '/bck1/data128/oradata/DBNAME/undotbs1_02.dbf',
................................................
  '/bck1/datadw29/oradata/DBNAME/corp/comm_sd37.dbf',
  '/bck1/datadw28/oradata/DBNAME/corp/comm_sd38.dbf',
.................................................................................................
  '/bck1/dataidxdw42/oradata/DBNAME/ods/ODS_DATA_IDX_07.dbf',
  '/bck1/dataidxdw42/oradata/DBNAME/ods/ODS_DATA_IDX_08.dbf'

I tried to use "v"i editor and "sed" to insert first part of path name. it can work on some lines and could not globally make change. I want to learn how to use "sed" to globally update the file path and deal with different number in path name and with different path name. Please help to provide your advice and command here. I will test and come back to report progress. Thanks for your time and help.
# 2  
Old 09-27-2019
How far would
Code:
sed 's#/#/bck1/#' file

get you?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-28-2019
RudiC:

I have tested. It works. your code can deal with all situation and insert new path text in the beginning of the all path. Thanks a lot. If you can give some explanation about "sed" options such as 's#/#/text/#', that will help more. I searched on line and could not get more information about these expression. Of course, I will do more search and learning for "sed".

Last edited by duke0001; 09-29-2019 at 12:50 AM..
# 4  
Old 09-29-2019
man sed and info sed are profound sources of information. info sed:



Quote:
The 's' command (as in substitute) is probably the most important in'sed' and has a lot of different options. The syntax of the 's' command is 's/REGEXP/REPLACEMENT/FLAGS'.


Its basic concept is simple: the 's' command attempts to match the pattern space against the supplied regular expression REGEXP; if the match is successful, then that portion of the pattern space which was matched is replaced with REPLACEMENT.


The '/' characters may be uniformly replaced by any other single character within any given 's' command. The '/' character (or whatever other character is used in its stead) can appear in the REGEXP or REPLACEMENT only if it is preceded by a '\' character.
# 5  
Old 09-30-2019
RudiC:

Thanks for explanation. I still do not understand. man sed, only tell us 's/REGEXP/REPLACEMENT/FLAGS'.. In your command, 's#/#/text/#', text is REPLACEMENT. So 1st #, 2nd # and 3rd # stand for what? Wildcard for path name? Are they extended expressio or something else? I checked online and other books. I could not find answer. Can you please explain your command in details? Sorry for this. I want to really know the programming logic of your command.
Thanks in advance for your help.
# 6  
Old 09-30-2019
Did you read and understand my quote from info sed?
Quote:
The '/' characters may be uniformly replaced by any other single character within any given 's' command. The '/' character (or whatever other character is used in its stead) can appear in the REGEXP or REPLACEMENT only if it is preceded by a '\' character.
So my command reads
Code:
s#pattern#replacement#

as the / is part of both pattern and replacement. The delimiter or separator is #, the pattern is /, and the replacement is /bck1/.
# 7  
Old 09-30-2019
RudiC:

Thanks a lot. Here # is delimiter. I thought the # is some extended expression and tried to find more information without success. Now, I learned from you. I will keep this explanation for the future usage. Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Insert a column in the beginning

Hi, I have been trying to see how i can insert a column in the beginning to my html table This is how it looks Name Age Sid 32 John 33 Mary 34 I want to insert a column Job before Name column, so it looks like Job Name Age IT Sid 32 Doctor... (3 Replies)
Discussion started by: sidnow
3 Replies

2. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

3. Shell Programming and Scripting

sed to insert a character

Hi all, I have a file like this Q8N302 21-84 Q8N157 15-45 Q99996 167-201 202-251 269-318 I want to insert a character or space if the line starts with a number and I used the command sed 's/^/#/' But in the output file, when it inserts this character, first digit in the number is... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

Insert output from a file to beginning of line with sed

Hi I've been trying to search but couldn't quite get the answer I was looking for. I have a a file that's like this Time, 9/1/12 0:00, 1033 0:10, 1044 ... 23:50, 1050 How do I make it so the file will be like this? 9/1/12, 0:00, 1033 9/1/12, 0:10, 1044 ... 9/1/12, 23:50, 1050 I... (4 Replies)
Discussion started by: diesel88
4 Replies

5. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

6. Shell Programming and Scripting

Need to insert text(constant) at the beginning of file

I am very new to scripting and I know this request is simple but I am having no luck with it. I have a file a.dat with the following data in it. aa bb cc dd I need to run a script that will take each line of a.dat and put dsjc/ubin/ in front of each record, so the output looks like ... (2 Replies)
Discussion started by: jclanc8
2 Replies

7. UNIX for Dummies Questions & Answers

Remove words beginning with a certain character from a file

Hi, how could you go about removing words that begin with a certain character. assuming that this character is '-' I currently have echo "-hello" | sed s/-/""/ which replaces the leading dash with nothing but I want to remove the whole word, even if there are multiple words beginning... (3 Replies)
Discussion started by: skinnygav
3 Replies

8. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

9. UNIX for Dummies Questions & Answers

How can I insert character to end of file?

Hello all How can I insert character to the end of text file without opening it in vi Just simple one liner, can it be done? Tnx (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Adding a character in the beginning of every line in a .dat file

How can i add a character(#) in the beginning of every line in a .dat file (2 Replies)
Discussion started by: Cool Coder
2 Replies
Login or Register to Ask a Question