Making substring with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making substring with sed
# 1  
Old 08-21-2010
Making substring with sed

Input:
Code:
You can easily do this by highlighting your code.

How can i get the substring from index 9 to 12 using sed?
# 2  
Old 08-21-2010
Quote:
Originally Posted by cola
Input:
Code:
You can easily do this by highlighting your code.

How can i get the substring from index 9 to 12 using sed?
Code:
$ sed "s/.\{8\}\(....\).*/\1/" file1
easi

# 3  
Old 08-21-2010
Quote:
Originally Posted by scottn
Code:
$ sed "s/.\{8\}\(....\).*/\1/" file1
easi

Ok,how to get substring from index 0 to index 15?
# 4  
Old 08-21-2010
Quote:
Originally Posted by cola
Ok,how to get substring from index 0 to index 15?
Aptly:

Code:
$ sed "s/\(.\{16\}\).*/\1/" file1
You can easily

I take 0 to 15 to mean 16 characters.
This User Gave Thanks to Scott For This Post:
# 5  
Old 08-21-2010
Quote:
Originally Posted by scottn
Aptly:

Code:
$ sed "s/\(.\{16\}\).*/\1/" file1
You can easily

I take 0 to 15 to mean 16 characters.
If i remove .* from the command then output is the original input string.Why is it?
Code:
sed "s/\(.\{16\}\)/\1/" file1

# 6  
Old 08-21-2010
Quote:
Originally Posted by cola
If i remove .* from the command then output is the original input string.Why is it?
Code:
sed "s/\(.\{16\}\)/\1/" file1

If you remove the ".*" part from the regex, that means you want to match a line that has only 16 characters, this simply does not match anything, so the original string is output.

=======================
I am sorry, my understanding was wrong.

Last edited by kevintse; 08-22-2010 at 12:05 AM.. Reason: Explanation is wrong.
This User Gave Thanks to kevintse For This Post:
# 7  
Old 08-21-2010
Quote:
Originally Posted by cola
If i remove .* from the command then output is the original input string.Why is it?
Code:
sed "s/\(.\{16\}\)/\1/" file1

Code:
[2addr]s/regular expression/replacement/flags
             Substitute the replacement string for the first instance of the regular expression in the pattern space.

If you leave out the .* then it isn't part of the regular expression (and won't be in the pattern space), and so won't get substituted.

Output is the original, but it was still substituted:

i.e. consider:

Code:
$ sed "s/\(.\{16\}\)/X /" file1
X o this by highlighting your code.

This User Gave Thanks to Scott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - replacing a substring containing a hyphen

I'm attempting to replace a substring that contains a hyphen and not having much success, can anyone point out where i'm going wrong or suggest an alternative. # echo /var/lib/libvirt/images/vm888b-clone.qcow | sed -e 's|vm888-clone|qaz|g' /var/lib/libvirt/images/vm888b-clone.qcow (1 Reply)
Discussion started by: squrcles
1 Replies

2. Shell Programming and Scripting

Substring Problem with sed . . .

Greetings. I'm looking to isolate the first occurrence of an arbitrary substring which may be present at any particular line in a given file. The enclosing end markers for the target in our thought problem are string" and ". The complete string and surrounding text could look something like... (3 Replies)
Discussion started by: LinQ
3 Replies

3. Shell Programming and Scripting

sed optimized for replace substring

Hi All, I have this text file IR7013903069171012INT10171211864 Axxxxxxxxxx12345dddddddddddd Byyyyyyyyy99999999ddddddddd IR7013903069171012TOS10171211865 Cffffffffff5745747ehdefffffffffgggggggg Dyyyyyyyyy99999999ddddddddd I need to 1) find lines where the substring IR70139 is... (5 Replies)
Discussion started by: gio123bgg
5 Replies

4. Shell Programming and Scripting

Using sed to find a substring from a file

HI All, I have a file which looks like below ./Prod_id/rel/prod/lib.a ./Some_text/rel/fld/lib.a ./Some_text/deb/detail/lib.a ./Some_text/deb/err/lib.a ./Some_text/rel/prod/lib.a ./Some_text/rel/fld/lib.a ./Some_text/deb/detail/lib.a ./Some_text/deb/err/lib.a I want... (5 Replies)
Discussion started by: anand.shah
5 Replies

5. Shell Programming and Scripting

Extract a substring using SED/AWK

Hi All, I have a log file in which name and version of applications are coming in the following format name It may look like following, based on the name of the application and version: XYZ OR xyz OR XyZ OR xyz I want to separate out the name and version and store them into variables.... (4 Replies)
Discussion started by: bhaskar_m
4 Replies

6. Shell Programming and Scripting

Substring using cut/awk/sed

Hi Gurus,I have a seemingly simple problem but struggling with it.It is as follows : I/p string - ABCDEFGHIJ20100909.txt desired o/p - AB,DEF,20100909,ABCDEFGHIJ20100909.txt How to achieve it ?Thanks in advance. Please use code tags, thank you (20 Replies)
Discussion started by: sumoka
20 Replies

7. Shell Programming and Scripting

Sed and SubString

Hi Folks, I am here with a simple doubt. I am having a flat file in which I want to replace the characters from say 5 to 15 as some text. Flat file contains a single line. For example 01MRRAJESH21000RAJESH INDUSTRIES In the above line pos 16-21 is Rajesh, I want to search for the... (4 Replies)
Discussion started by: dinesh1985
4 Replies

8. Shell Programming and Scripting

Substring using sed or awk

I am trying to get a substring from a string stored in a variable. I tried sed with a bit help from this forum, but not successful. Here is my problem. My string is: "REPLYFILE=myfile.txt" And I need: myfile.txt (everything after the = symbol). My string is: "myfile.txt.gz.20091120.enc... (5 Replies)
Discussion started by: jamjam10k
5 Replies

9. Shell Programming and Scripting

Using sed to get a substring

Hi, I have looked all over for this. I am attempting to get a the substring of a string using sed since it seemed the best solution for this. For example my string is: "zzz foo to you and bar123 or foo" I would like to extract the text between "and" and "or" (it could be anything, but... (2 Replies)
Discussion started by: CentaurAtlas
2 Replies

10. Shell Programming and Scripting

Sed extract substring on (OS X)

On OS 10.4.11 I have filenames like: 670711 SA T2 v1-1_DS_EF.doc CT_670520 AM T1 v1-2_DS_EF.doc CT_670716 - 2 SA T4 v1-2_DS_EF.doc CT_670713 SA T3 v1-1_DS_EF.doc 670421 PA DYP1 v1-1_DS_EF.doc CT_670425 PA DYP2 v1-1_DS_EF.doc CT_670107 RA T3 v1-2_DS_EF.doc CT_670521 AM T2 v1-2_DS_EF.doc... (3 Replies)
Discussion started by: mlommel
3 Replies
Login or Register to Ask a Question