Replace words with the first characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace words with the first characters
# 1  
Old 10-03-2010
Replace words with the first characters

Hello folks, I have a simple request but I can't find a simple solution.
Hare is my problem. I have some dates, I need to replace months with only the first 3 characters (jan for january, feb for february, ... all in lower case)
Code:
~$ echo '3 october 2010' | sed [script of some sort]
3 oct 2010

I thought of something along this (at least to extract the 3 characters) but It doesn't work the way I want:
Code:
echo 'today is 3 october 2010' | sed -r '/(january|february|march|april|june|july|august|september|october|november|december)/{s/^(...).*/\1/}'

Any ideas?
# 2  
Old 10-03-2010
Code:
echo '3 october 2010' | awk '{$2=substr($2,1,3)}1'

# 3  
Old 10-03-2010
Code:
echo '3 october 2010' |  awk '{print $1,tolower(substr($2,0,3)),$3}'

# 4  
Old 10-03-2010
Thanks for your suggestions.
But the month can actually be anywhere in the phrase.
I'm searching around awk's gsub command for now but can't yet find something useful.
Code:
echo '3 october 2010' | awk 'gsub(/january|february|march|april|june|july|august|september|october|november|december/,[missing part^^]

# 5  
Old 10-03-2010
Code:
echo 'today is 3 october 2010' | 
sed -r 's/(jan)uary/\1/g;s/(feb)ruary/\1/g;s/(mar)ch/\1/g;s/(apr)il/\1/g;s/(jun)e/\1/g;s/(jul)y/\1/g;s/(aug)ust/\1/g;s/(sep)tember/\1/g;s/(oct)ober/\1/g;s/(nov)ember/\1/g;s/(dec)ember/\1/g'


Last edited by Scrutinizer; 10-03-2010 at 06:08 AM.. Reason: added global replace
# 6  
Old 10-03-2010
Code:
perl -lpe 's/(january|february|march|april|june|july|august|september|october|november|december)/substr($1, 0, 3)/eg'

# 7  
Old 10-03-2010
Thank you Scrutinizer and alister, those two suggestions fit my needs well.
I had hopes with something more "generic" or a helluva short awk script to do this, but... nevermind ^^
Finally, I think I'll keep my current approach, using bash's "Parameter Expansion":
Code:
        d=${d//january/jan};d=${d//february/feb};d=${d//march/mar};
        d=${d//april/apr};d=${d//june/jun};d=${d//july/jul};
        d=${d//august/aug};d=${d//september/sep};d=${d//october/oct};
        d=${d//november/nov};d=${d//december/dec}

Thank you again :)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

3. Shell Programming and Scripting

Perl: Pattern to remove words with less than 2 characters.

Hello. I've been thinking about how to go about this. I know I'm close but still does not work. I need to remove any word in that is not at least 2 characters long. I've removed all the non-alphabetic characters already (numbers included). Here's an example: my $string = "This string is a... (4 Replies)
Discussion started by: D2K
4 Replies

4. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

Get characters between two words

Guys, Here is the txt file... SLIC N0SLU704034789 rŒ° EJ00 ó<NL DMRG>11 100 4B 2 SLIC N0SLU704034789 rŒ° TJ10 <4000><NL> 2 SLIC N0SLU704034789 ... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Programming

Counting characters, words, spaces, punctuations, etc.

I am very new to C programming. How could I write a C program that could count the characters, words, spaces, and punctuations in a text file? Any help will be really appreciated. I am doing this as part of my C learning exercise. Thanks, Ajay (4 Replies)
Discussion started by: ajay41aj
4 Replies

8. Shell Programming and Scripting

deleting symbols and characters between two words

Hi Please tell me how could i delete symbols, whitespaces, characters, words everything between two words in a line. Let my file is aaa BB ccc ddd eee FF kkk xxx 123456 BB 44^& iop FF 999 xxx uuu rrr BB hhh nnn FF 000 I want to delete everything comes in between BB and FF( deletion... (3 Replies)
Discussion started by: rish_max
3 Replies

9. Shell Programming and Scripting

Script for pulling words of 4 to 7 characters from a file

Even just advice on where to start would be helpful. Thank You (2 Replies)
Discussion started by: Azeus
2 Replies

10. Shell Programming and Scripting

Display text between two words/characters

Using sed or awk, I need to display text between two words/characters. Below are two example inputs and the desired output. In a nutshell, I need the date-range value between the quotes (but only the first occurance of date-range as there can be more than one). Example One Input: xml-report... (1 Reply)
Discussion started by: cmichaelson
1 Replies
Login or Register to Ask a Question