sed to insert a slash and keep text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to insert a slash and keep text
# 1  
Old 05-31-2011
sed to insert a slash and keep text

I have:
/path/to/my/fixdir/MD1234567.tar

I want to have:
/path/to/my/fixdir/MD/1234567.tar

fixdir never changes but MD does and how many numerical digits does. I want something like:
/usr/bin/sed 's/fixdir\/../fixdir\/..\//'

This ends up:
/path/to/my/fixdir/../1234567.tar

But instead of it putting in two periods I want it to put the characters that were there back. I am on Solaris and want to use sed.

Thanks
# 2  
Old 05-31-2011
Code:
echo '/path/to/my/fixdir/MD1234567.tar' | sed 's#\(fixdir/\)\(..\)#\1\2/#'

# 3  
Old 06-01-2011
Quote:
Originally Posted by crowman
fixdir never changes but MD does and how many numerical digits does. I want something like:
/usr/bin/sed 's/fixdir\/../fixdir\/..\//'

This ends up:
/path/to/my/fixdir/../1234567.tar

But instead of it putting in two periods I want it to put the characters that were there back. I am on Solaris and want to use sed.

Thanks
Try the below sed..
Code:
echo '/path/to/my/fixdir/MD1234567.tar'|sed 's/fixdir\/\([A-Z][A-Z]*\)\([0-9][0-9]*.*$\)/fixdir\/\1\/\2/'

# 4  
Old 06-01-2011
Try that one :

Code:
$ echo '/path/to/my/fixdir/MD1234567.tar' | sed 's:.*/..:&/:'
/path/to/my/fixdir/MD/1234567.tar

the & reference the pattern that have previously been matched and using colon as separator avoid escaping the slash

Last edited by ctsgnb; 06-01-2011 at 04:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to insert text between lines

Hello, I am trying to insert a section of text between lines in another text file. The new lines to be inserted are: abcd.efgh.zzzz=blah abcd.efgh.xxxx=blah Where N = 0 to 2 Original File: abcd.efgh.wwxx=aaaaa abcd.efgh.yyzz=bbbbb abcd.efgh.wwxx=aaaaa abcd.efgh.yyzz=bbbbb... (3 Replies)
Discussion started by: tsu3000
3 Replies

2. Shell Programming and Scripting

How do I insert text with sed ?

Hi I was wondering if anyone new of a solution to this problem? I need to copy a time stamp that is on a line of .text in a text file into multiple positions on the same line. I need to insert the time stamp on the same line between every occurance of the text ".pdf_.html" right after the... (9 Replies)
Discussion started by: Paul Walker
9 Replies

3. Shell Programming and Scripting

Insert text using sed

sed 's/$/TEST/g' will insert TEST at the end of each line. i want to insert TEST at column 64 (7 Replies)
Discussion started by: lawsongeek
7 Replies

4. Shell Programming and Scripting

sed insert text without newline

Hi, I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file: > cat myfile This is first line. > sed -i '1i\foo' myfile > cat myfile foo This is first line. ... (5 Replies)
Discussion started by: tdw
5 Replies

5. Shell Programming and Scripting

Insert text with Sed (in various positions)

Hello. I'm trying to insert text in various positions and I could only do that using pipes for each position. Example: cat file | sed -e 's#\(.\{5\}\)\(.*\)#\1:\2#g' | sed -e 's#\(.\{26\}\)\(.*\)#\1:\2#g' Insert ":" at position 5 and 26. it can be done in the same sentence, without using... (4 Replies)
Discussion started by: </kida>
4 Replies

6. Shell Programming and Scripting

Insert text file only after the first match with SED

Hello, I'm new in Shell scripting but i should write a script, which inserts the license header out of a txt-File into the files in our Projekt. For the Java classes it runs without Problems but for XML files not. At xml-files i have to put the license Header after the xml-Header (?xml... (1 Reply)
Discussion started by: PhoenixONE
1 Replies

7. Shell Programming and Scripting

Using sed to append backward slash before forward slash

Hi all, I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front... (2 Replies)
Discussion started by: sarbjit
2 Replies

8. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. UNIX for Dummies Questions & Answers

Insert Text With Sed

Hello. Trying to insert text at line 1 and after last line of file. I have searched posts but nothing seems to work. I keep getting extra characters error or nothing gets inserted into the file. #!/bin/sh touch textfile.txt sed 'i\ Add this line before every line with WORD' textfile.txt ... (5 Replies)
Discussion started by: steveramsey
5 Replies
Login or Register to Ask a Question