Sed replacing words with abbreviations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed replacing words with abbreviations
# 1  
Old 03-03-2009
Sed replacing words with abbreviations

I really hate to do this, but I am completely stumped. I have to create a sed script that will change the abbreviations in a file to the full word. I really just have no idea where to start. All I want is a starting point as well no actual complete answer. Thank you for your time in advance.

~mauler123
# 2  
Old 03-03-2009
Nevermind I figured it out I did

Code:
cat addresses.txt | \sed -e 's/Ave./Avenue/' | \sed -e 's/St./Street/' | \sed -e 's/E./East/' | \sed -e 's/Ter./Terrace/' > addresses4.txt

and that worked for what I needed to do. Although after looking at my solution I don't think I posted in the right section, sorry!
# 3  
Old 03-03-2009
Hi there,
I think this was the correct forum. At least it's the one I use.
If your not very familiar with sed, here are some remarks:
1) You can put all the replacements in one sed
Code:
cat addresses.txt | sed -e 's/Ave./Avenue/; s/St./Street/; s/E./East/; s/Ter./Terrace/' > addresses4.txt

2) Without the g option, only the first occurence of the string in each line will be replaced. I know it's not very likely to have "Ave." appear two times in an address, but you'd better be sure
Code:
cat addresses.txt | sed -e 's/Ave./Avenue/g; s/St./Street/g; s/E./East/g; s/Ter./Terrace/g' > addresses4.txt

3) You don't need to cat the file, just give it as an argument
Code:
sed -e 's/Ave./Avenue/g; s/St./Street/g; s/E./East/g; s/Ter./Terrace/g' addresses.txt > addresses4.txt

4) And if you want to replace the file itself, use the i switch
Code:
sed -i 's/Ave./Avenue/g; s/St./Street/g; s/E./East/g; s/Ter./Terrace/g' addresses.txt

5) Beware that a period (.) is a special character that represent any other one. Which means that Aven, Avel, Aved all match Ave. and will be replaced by Avenue. Escape the period to match an actual period.
Code:
sed -i 's/Ave\./Avenue/g; s/St\./Street/g; s/E\./East/g; s/Ter\./Terrace/g' addresses.txt

I hope I helped
Santiago
# 4  
Old 03-03-2009
Thanks for the info. I used a cat and just read the file first first so I knew exactly what I needed to replace beforehand, which is why what I used worked.

All of the other information was a lot of help thanks!
# 5  
Old 03-03-2009
Take a read: UUOC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. UNIX for Dummies Questions & Answers

Replacing word and Capitalize words after

I have an assignment and I am not sure what to do. In Unix, I use PuTTY change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it. I know change command is M-% and "." so only one semicolon is changed but I am not sure how to... (1 Reply)
Discussion started by: kathrut43
1 Replies

3. Shell Programming and Scripting

Replacing words in file

Hello All Probably this is very simple for you but I cant figure it out I have to replace "No Header" with "Output Field Names" I/P file <ATTRIBUTE NAME ="Header Options" VALUE ="No Header"/> O/P needed <ATTRIBUTE NAME ="Header Options" VALUE = "Output Field Names"> (4 Replies)
Discussion started by: Pratik4891
4 Replies

4. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

5. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

6. Shell Programming and Scripting

sed append words

Hi all, I have a file like one two three for five six seven eight ..... Actually i need to append a label to the words that belong to the 2 column and get: one two_label three for five six_label seven eight .... I was trying with sed inside vim but I can't figure out... (9 Replies)
Discussion started by: Dedalus
9 Replies

7. Shell Programming and Scripting

Replacing words

Hi, I have am using a file that contains names that I want to replace. Basically file 1 looks like this jack joe james john I have another file (file 2) that looks like this jack 2345 joe 6848 james 3342 john 3432 Basically I want to replace column1 from file1... (4 Replies)
Discussion started by: kylle345
4 Replies

8. Shell Programming and Scripting

Replacing words in a fast way

Hi, I have a file that looks like this: br0 br0 br1 br10 br11 br12 br13 br14 br15 br15 br2 br2 br3 br4 br5 br6 br7 (5 Replies)
Discussion started by: phil_heath
5 Replies

9. UNIX for Dummies Questions & Answers

sed [delete everything between two words]

Hi, I have the following codes below that aims to delete every words between two pattern word. Say I have the files To delete every word between WISH_LIST=" and " I used the below codes (but its not working): #!/bin/sh sed ' /WISH_LIST=\"/ { N /\n.*\"/ {... (3 Replies)
Discussion started by: Orbix
3 Replies

10. Programming

Replacing words in a file

I'm trying to write a program that will open an existing file supplied by the command line argument and then replace words with "We" or "we" by "I" and "a" or "A" by "The". When I run the program it reads the file, changes the word but re writes it on a new line with only the replaced words not the... (1 Reply)
Discussion started by: adam85
1 Replies
Login or Register to Ask a Question