Sed Newbie Question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed Newbie Question
# 1  
Old 05-26-2011
Sed Newbie Question

Hello everyone

I could really need some help. I want to rename : with dot. But the problem is that i don't want to touch the hours like 12:00 but i want to change AD:FG with AD.FG. I'm thinking something like [A-Z]: to [A-Z].

I don't know if i explain it corrrectly but i could use some help

Thanks in advance
# 2  
Old 05-26-2011
The basic pattern for sed substitutions is:

Code:
s[delimiter]<pattern>[delimiter]<replacement pattern>[delimiter]<options>

The delimiter is any single character, you just have to use the same throughout the command. Traditionally the slash ("/") is used, but you could use any other either. The "pattern" is what you look for, the "replacement pattern" is what you want to replace it with.

Per default only the first occurrence ina line is replaced, if you want to change every occurrence of the pattern in a line you have to add the option "g" (for "global").

As an example: change the first "a" to "b" ("aaaaa" will become "baaaa"):

Code:
s/a/b/

change every "a" to "b":

Code:
s/a/b/g

It would be easy to apply this to your problem and write as a search pattern: "[A-Za-z]:". Unfortunately things are a litle bit more complicated: we will have to "remember" in the replacement pattern which character we have matched in the search pattern. "A:" should be replaced by "A.", but "B:" by "B.", etc.

There is a device in sed which can accomplish this. It is like defining a variable and using its content in the replacement pattern: The parts in the search pattern we enclose in "\(" and "\)" are being remembered and we can use "\1" ("\2", ...) in the replacement pattern to call these.

Here is an example:

Code:
s/\([A-Za-z]\)/=\1=/g

every single character is replaced by itself, surrounded by equal signs. Input: "a b c d" Output: "=a= =b= =c= =d=".

Applying this to your problem:

Code:
s/\([A-Za-z]\):/\1./g

I hope this helps.

bakunin
# 3  
Old 05-26-2011
Thanks for the quick answer but it doesn't work.

i run
Code:
sed -i -e s/\([A-Za-z]\):/\1./g

but didn't change. In order to be sure that i explained it correctly and don't eat your time for nothing, i want to change OD:OD to OD.OD but not touch 12:00 that is in a previous line

Thanks again
# 4  
Old 05-26-2011
Quote:
Originally Posted by zoe
i run
Code:
sed -i -e s/\([A-Za-z]\):/\1./g

but didn't change.
Hmm...

Your command looks good, but i miss quoting: many of the characters used in sed-expressions are special to the shell and therefore it is very advisable to enclose EVERY sed-statement in single quotes:

Code:
sed 's/\([A-Za-z]\):/\1./g' /path/to/inputfile

Issue the statement as it is, it will display its results on screen (you might pipeline it to "more" to read it). Once you are satisfied with the results redirect the output to another(!) file, then "mv" the output over the original input.

Using "-i" is an extremely dangerous options, because if the sed statement does something other than expected your input file will be corrupted.

I hope this helps.

bakunin
# 5  
Old 05-26-2011
Code:
$ echo "12:00 but i want to change AD:FG with AD.FG" | sed 's/\([[:alpha:]]\):/\1./g'
12:00 but i want to change AD.FG with AD.FG

# 6  
Old 05-26-2011
You absolutely correct .it works but i must find a way to make it work with unicode. I use cygwin to test it, but because the characters are in greek i could get the correct result. So i tested in shell like that
Code:
echo "12:00 OD:OD") | sed 's/\([A-Za-z]\):/\1./g'

and worked. Now i have to see how to make it work from batch using greek letters.

Anyway, thanks for the time and the correct answer
# 7  
Old 05-26-2011
You could alternately use the negation of digit :

Code:
$ echo "12:00 OD:OD" | sed 's/\([^0-9]\):/\1./g'
12:00 OD.OD
$ echo "12:00 OD:OD" | sed 's/\([^[:digit:]]\):/\1./g'
12:00 OD.OD

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed newbie question

hi all: i am trying to use sed to separate hex bytes "363834574e292c20" to "36 38 34 57 4e 29 2c 20". i used the following script (googled) and i got "3 63 83 45 74 e2 92 c2 0". how could i modify the script to meet my needs? thanks (8 Replies)
Discussion started by: ipfreak
8 Replies

2. Shell Programming and Scripting

What does this do (newbie question)...

I was looking through some code online and came accross this... ls *.txt | grep text1 | cat file1 – file2 | `echo wc –l` I know what "ls|grep text1" does and I know a word count gets echoed but beyond that I am confused. Please use layman terms as much as possible as I am a newbie. (8 Replies)
Discussion started by: elohssa
8 Replies

3. UNIX for Dummies Questions & Answers

newbie question

Hi all, I am sure this is very simple but I cant quite get it. I am trying to search textfile1.txt for a string then take the results of the search and append the result to textfile3.txt So far I have used $ find file1.txt -exec grep "string i am looking for" '{}' \; -print this... (2 Replies)
Discussion started by: radgator
2 Replies

4. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

6. Shell Programming and Scripting

SED question: newbie here.

Hello... I wanted to figure out how to remove the 1st instance of a comma from a file.. 100+ line file ------------- 'test1' ,'dudes are cool' <-- remove comma from first instance of comma in file ,'cool dude' ,'bbbbbb' I tried everything from cat jigar|tr , >1.txt to cat jigar|sed... (2 Replies)
Discussion started by: jigarlakhani
2 Replies

7. UNIX for Dummies Questions & Answers

Newbie question?

What is the best way to learn UNIX on the web, with out buying books? any link would be much help. Thank you in advance, L (1 Reply)
Discussion started by: lsoria1
1 Replies

8. UNIX for Dummies Questions & Answers

newbie question

I am taking a db classes toward oracle 10g. I am taking unix as well . I need to know what is the best option for os . should I use linux fedora. or get a sun box and start learning from there. Thanks (6 Replies)
Discussion started by: xzyan
6 Replies

9. Shell Programming and Scripting

newbie question

hey all, I have repeatedly seen scripts containing the following syntax, grep "hello" $myfile >> $log 2>&1 can anyone explain exactly what "2>&1" mean? THANK YOU (4 Replies)
Discussion started by: mpang_
4 Replies

10. UNIX for Dummies Questions & Answers

Very new newbie question

sorry if im not asking inthe right spot but, how do you turn the beeping off every time you hit a key onthe keyboard. I tried the click -n but it told me it didnt recognize click any help would be greatly appreciated ( the beeping is not going over well in the surrounding cubicles) thank you... (4 Replies)
Discussion started by: Split100
4 Replies
Login or Register to Ask a Question