Visit Our UNIX and Linux User Community


Sed capture help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed capture help
# 1  
Old 04-28-2011
Sed capture help

Hi, I'm a newbie to sed and learning about sed 1 liners and captures. I was wondering if someone could help me with the code below:

Image

So that's taking a URL and splitting it into URL,TLD & Filename. Any help on how I can do this is much appreciated.

Apologies for posting an image. I don't have enough post count to insert a URL.
# 2  
Old 04-28-2011
Code:
echo "www.google.com/sample.log" |awk -F \/ '{print $0,$1,$2}'

# 3  
Old 04-29-2011
nice awk solution but OP asked for sed:

Code:
echo "www.google.com/sample.log" |sed -r 's/(.*)\/(.*)/\1\/\2 \1 \2/g'

Explaination:

Breaking this sed line down we have 4 main parts.

1. The sed command and it's flags
- The -r tells sed to use regex-extended matching.

2. The search pattern
- (.*)\/(.*)
- \/ is the escaped form of /
- We escape the '/' since sed sees '/' as a search pattern delimiter

3. The replace pattern
- \1\/\2 \1 \2
- \/ is the escaped form of /
- We escape the '/' since sed sees '/' as a search pattern delimiter

4. The sed commands
- 's' at the start
- This tells sed that we are searching and replacing
- The character following s used as the pattern delimiter
- You can make the delimiter what ever you like (|, /, :, ^, etc...)
- 'g' at the end
- This tells sed to match more than one occurrence per line if found
- If not used and the line has multiple occurrences of pattern only 1 is matched and replaced

The (.*)\/(.*) says match everything but keep the parts before and after the / (which is escaped as \/) for later use.

In the replace pattern of the search we use \1 and \2 several times along with the escaped / (\/) again.

\1 is the first item we kept from the search (everything to the left of '/' or www.google.com)
\2 is the second part we kept (everything to the right of '/' or sample.log) .

So '\1\/\2 \1 \2' becomes 'www.google.com/sample.log www.google.com sample.log'

Last edited by ddreggors; 04-29-2011 at 12:28 PM..
These 2 Users Gave Thanks to ddreggors For This Post:
# 4  
Old 04-29-2011
Beautiful. Thank you both & special thanks to ddreggors for the wonderful explanation.
# 5  
Old 04-29-2011
Like ddreggors have mentioned we can use any other character as a delimiter instead of "/" in sed. This will help in avoiding confusion if we have "/" in the string we are using. Below is a slightly modified code using "!" as delimiter.

Code:
#using !
echo "www.google.com/sample.log" | sed 's!\(.*\)/\(.*\)!\1 \2!'

#using =
echo "www.google.com/sample.log" | sed 's=\(.*\)/\(.*\)=\1 \2='

regards,
Ahamed
# 6  
Old 04-29-2011
Also note that Ahamed did not use a 'g' at the end of the regex search/replace. In this situation (mine included) the g is not needed.

Here is why:

With a match like we have '(.*)\/(.*)' we would match the whole line. Everything before the '/' and everything after.

Now take the following line:

ABC123ABC456ABC

if sed matches ABC and replaces it with XYZ but does not use the g command (sed 's/ABC/XYZ/') only the first ABC would be replaced.

Examples:
Code:
$ echo "ABC123ABC456ABC" |sed 's/ABC/XYZ/'
XYZ123ABC456ABC
$ echo "ABC123ABC456ABC" |sed 's/ABC/XYZ/g'
XYZ123XYZ456XYZ

# 7  
Old 04-29-2011
Quote:
Originally Posted by ddreggors
- The character following s used as the pattern delimiter
- You can make the delimiter what ever you like (|, /, :, ^, etc...)
Quote:
Originally Posted by ahamed101
Like ddreggors have mentioned we can use any other character as a delimiter instead of "/" in sed.
BEGIN PEDANTRY

Except for backslash and newline.

END PEDANTRY

Smilie

Regards,
Alister

Previous Thread | Next Thread
Test Your Knowledge in Computers #1012
Difficulty: Easy
Hexadecimal number are represented using base 8 times 2.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to capture the year

Hi, I'm not real familiar with sed, but I need to capture the year this directory path. Here's what I have so far. I'm able to capture the run name, but I want the only the year that's within the run name. How can I do this? ... (3 Replies)
Discussion started by: jdilts
3 Replies

2. Shell Programming and Scripting

Capture the value between brackets

Hi I am having hard time getting this right and need some help. I Have 3 files, and everyone contains the following:- File1 Today, we have Name(Jack) Age(19) Class (A2) Today, we have Name(Kim) Class (G9) File2 Today, we have Name(Lee) Age(19) Class (A2) Today, we have... (8 Replies)
Discussion started by: samsan
8 Replies

3. Programming

how to capture OS name using C/C++ code

Hi, I want to know the os name via c/c++ source code.so please help me to do the same. I will appreciate if anyone can provide me the source code. Thanks in advance.. (9 Replies)
Discussion started by: smartgupta
9 Replies

4. Shell Programming and Scripting

script to capture

Hi In my production server some user runnig some scripts to get some data. I need a script to capture this user script code. best rds ab (5 Replies)
Discussion started by: aboorkuma
5 Replies

5. Shell Programming and Scripting

Capture of month

Guys I need your help... I receive the month and the day in a file and I keep it in a variable. I need to take this day that Iīve kept compare with the current day and pick the file to the next day... Letīs say I receive a file 27-JUL-2007 and I need to check in the system the current day and... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

6. Shell Programming and Scripting

How to scan and capture

Hi, I am new to unix. I have a file with records like the below ads-sap-4.txt: </a></b></span><span class="linkbutton yellow_but"><a id="2005754_more" style="cursor:pointer; cursor:hand;"... (3 Replies)
Discussion started by: akondeti
3 Replies

7. Shell Programming and Scripting

capture the file name

I am trying to capture the file name(which is not of fixed length) and put it in a variable. It is working in unix whereas when I am running the same script in Informatica it is not giving me the desired output. But when I comment the option(find the file name) then it is working fine. It can... (4 Replies)
Discussion started by: Mandab
4 Replies

8. Shell Programming and Scripting

capture in telnet

I'm trying to capture the files that this command types out, but i'm not able to do it. I'm pipeing everything to the telnet command because if i dont the connection closes. Any suggestions. #!/bin/ksh hostn='x.x.x.xx xxxx' (echo "open $hostn\r" sleep 3 echo " \r" sleep 2 (echo... (3 Replies)
Discussion started by: wisher115
3 Replies

9. AIX

Capture IP Adress

hello I need for a script to capture the ip address from the connected user. I have 5 logical partitions. With "who", i have the ip adress, but only for 2 servers. Do you know another command to know the ip address of connected clients ? thank you (14 Replies)
Discussion started by: pascalbout
14 Replies

10. Shell Programming and Scripting

capture the ouput!

Hi, my perl script is calling another external java program. The Java in turn prints out a string. how can I capture the string. ------------------ #!/usr/bin/perl print "Content-type:text/html\n\n"; use CGI; $query = new CGI; $theCookie = $query->cookie('someCookie'); $user =... (0 Replies)
Discussion started by: azmathshaikh
0 Replies

Featured Tech Videos