How to get a string between two delimeters using sed.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get a string between two delimeters using sed.
# 1  
Old 03-06-2011
How to get a string between two delimeters using sed.

I want to get IP address only from the input using sed. i.e string between between "(" and ")" delimeters.

Code:
 
echo "serverA (11.22.333.444) is alive" | sed 's/<??>/<??>/'

I know how to do this using cut and awk's split features.

I also want to know how to do this using awk's multiple delimeter seperator syntax using something like FS="[(|)+]" but its not working [ echo "serverA (11.22.333.444) is alive" | gawk '{ FS = "[()]+" } { print $1 }' ].
# 2  
Old 03-06-2011
Code:
sed 's/.*(//; s/).*//'

Code:
awk -F '[()]' '{print $2}'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 03-06-2011
Code:
echo "serverA (11.22.333.444) is alive" |grep -Eo '[0-9]{2}.[0-9]{2}.[0-9]{3}.[0-9]{3}'
11.22.333.444

# 4  
Old 03-06-2011
Thanks alister, both sed and awk are working perfect.

yinyuemi,
Specific version of grep/egrep/fgrep I have on Solaris8 even under /usr/xpg4/bin does not have -o option available. Thanks for posting,, I did not know we can specify number characters using {#N} notation.. I am wondering if that is valid in regex for sed, awk also.!?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace last 9 delimeters “,” with “|” in UNIX

Hi Guys, I want to replace last 9 "," delimeters with "|" in a file Example :- "ashu,pant",3,5,5,7,7,87,8,8,8 "ashu,pant"|3|5|5|7|7|87|8|8|8 Help would be really appreciated. Thanks guys, Please use CODE tags as required by forum rules! (7 Replies)
Discussion started by: himanshupant
7 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

Lines between specific delimeters

Hi, I have a requirement like this Line 1 Line 2 Line 3 Manager Line 4 Line 5 I have to print like this Have you done your work on time because I have to forward the work forward. Any Help is really Appriciated.... (1 Reply)
Discussion started by: abhishek7687
1 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

Commas within Delimeters

Hi experts, I would like a favour from you guys to get the info from 5th column which was separated by the delimeter comma ( , ) The Data file is as below:- 1,USER1,"90, TEST AVENUE, OLD ROAD",test1,124,N 2,USER2,88 TEST STREET NEW ROAD,test2,123,N The User File is as below:- USER1... (1 Reply)
Discussion started by: shenkz
1 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

awk with column delimeters

Hi All, I want to execute this cat /etc/passwd | awk -F: '{print $1,$7}' with a fixed column on the first and second. Say on the first I want 12 column and second with 20 column. Example root /usr/bin/ksh lp /bin/false Thanks for any comment you may add. (5 Replies)
Discussion started by: itik
5 Replies

9. Shell Programming and Scripting

awk with two delimeters

Hi, can anyone explain me below codes...i used the below one to use two delimeter(://) at a time.. but i am not able to understand the output options.. note: here there is no requirement ..only to understand myself about below command... command: awk -F "" '{print $1}' inputfile ... (2 Replies)
Discussion started by: Shahul
2 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question