Reg: Query in sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reg: Query in sed
# 1  
Old 06-10-2008
Reg: Query in sed

Hi Penchal,


I would appreciate if you can provide me a brief explanation on what you are trying to do in the commnad below.


echo "6-9-2008" | sed 's/\(.\)-\(.\)-\(.*\)/\3-0\2-0\1/g'

Thanks
Amit
# 2  
Old 06-10-2008
Quote:
Originally Posted by patelamit009
Hi Penchal,


I would appreciate if you can provide me a brief explanation on what you are trying to do in the commnad below.


echo "6-9-2008" | sed 's/\(.\)-\(.\)-\(.*\)/\3-0\2-0\1/g'

Thanks
Amit
The input "6-9-2008" gets converted to "2008-09-06". Here is how it happens.

The sed statement takes the form 's/pattern/replacement/flags'

Here pattern is \(.\)-\(.\)-\(.*\)
replacement is \3-0\2-0\1
and flags is g

In the sed world, a . refers to any character. A .* refers to a collection of characters.
When you do \(.\), you are collecting a single character into a buffer. Hence \(.\)-\(.\)-\(.*\) will put 6 into the first buffer, 9 into the second and 2008 into the third.
Each of these buffers can be accessed within the replacement space by using the notation \1 for the first buffer, \2 for the second and so on and so forth.
When you do \3-0\2-0\1 in the replacement space, you create an output as 2008-09-06.
The 'g' flag tells sed to make the change global i.e. if there are more occurrences, all of them should be considered and changed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for query

Hi all, I am trying to remove quite a lot of numbers for a file I have which looks something along the lines of; 1,2,3,4,5,6 etc I have a list of numbers I want removing looking like; 10000 10987 16572 etc etc and have been trying to run; for id in `cat list` ; do sed -i -e... (8 Replies)
Discussion started by: JayC89
8 Replies

2. OS X (Apple)

Sed query

Hello Im fairly familiar with using the sed command for substitution, however I have been passed a script which checks the logged on username and directory type with a sed section which I cant figure out. The sed function has me baffled and I cant find out from the man page what its trying to do... (1 Reply)
Discussion started by: costaanglais
1 Replies

3. Shell Programming and Scripting

sed query

I have a sed query. There is a line which has tilde and I want to separate this line using sed. The line is: ABP_ETC_ROOT=~xdmadm The query to get this line is: sed -n '/\(.*\)~\(.*\)/p' infile I want to get xdmadm from this line and I am using this sed command: sed -n... (5 Replies)
Discussion started by: som.nitk
5 Replies

4. UNIX for Dummies Questions & Answers

Please help me out reg syb15 query

When i run the below query in syb15 (with syb 12.5.X backward compatibilty) environment it runs 45min where as the same in syb12.5.1 it takes only 7-10min. But the main thing is stld_date(in the below query) does not covered in the index of that table. Also main_table is a huge table. So is it... (0 Replies)
Discussion started by: prsam
0 Replies

5. Shell Programming and Scripting

sed query

hi i had posted this earlier.. got no reply !! how to change assigned value in a file using sed suppose the file contains age = 30; how to change it to age =50; i tried sed 's/^age*./age =50;' filename but i am getting the o/p as age =50; 30; plz hlp!! (4 Replies)
Discussion started by: gopsman
4 Replies

6. Shell Programming and Scripting

reg exp for sed

$ cat file.txt asd <AA>dev <LL>def <RR>sha This works for me: $ sed -r 's/^ .*<LL>def/\t<LL>my/' file.txt asd <AA>dev <LL>my <RR>sha But, this does not work for me: $ sed -r 's/^\s+<LL>def/\t<LL>my/' file.txt asd ... (1 Reply)
Discussion started by: demoprog
1 Replies

7. Shell Programming and Scripting

usage...sed/awk/reg-exp ..in shell scripting

in shell scripting there is extensive usage of i> regular expression ii>sed iii>awk can anyone tell me the suitable contexts ...i mean which one is suitable for what kind of operation. like the reg-exp and sed seems to be doing the same job..i.e pattern matching (1 Reply)
Discussion started by: mobydick
1 Replies

8. Shell Programming and Scripting

sed query

I have a series of folders /temp/a /temp/b /temp/c In folders a, b, and c, I have files a1.txt..........a20.txt b1.txt..........b40.txt & c1.txt..........c60.txt Each file has the same data format :- Line 1 AAAAA aaaa Line 2 BBB bbbbbb Line 3 CCCC cccccc Etc etc I need to write a... (13 Replies)
Discussion started by: grinder182533
13 Replies

9. Shell Programming and Scripting

sed query..

hi, I have an xml file and I need to replace the tags with different names all at a time here is what I have <cevalue> <cevalue1> <cevalue2> <cevalue3> <cevalue4> <cevalue5> and I need these like these... <cevalue> <cevalue> <cevalue> <cevalue> <cevalue> <cevalue> I tried a few but... (2 Replies)
Discussion started by: mgirinath
2 Replies

10. UNIX for Advanced & Expert Users

query reg semaphore / processes on solaris

Our Baan application has a server process bflusher which is activated automaticaly by another server process bmanager . These processes uses shared memory to carry out it's operations . Through a baan application query command (tbase6.1 P d 3 ) , i can find the number of users (user process... (1 Reply)
Discussion started by: Hitesh Shah
1 Replies
Login or Register to Ask a Question