Using Sed to perform multiple substitutions?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Sed to perform multiple substitutions?
# 1  
Old 04-15-2010
Using Sed to perform multiple substitutions?

Hello

I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end.

Code:
 
bash-3.00$ ccm query -n cmsynergy_menu.sh -f "%displayname %create_time" | nawk -F" " '{print $7 $4 $5 $6}' | sed 's/://g'

2008May12114734
2008May12115152
2008Aug19114242
2008Dec05110053
2008Dec05115352
2009Jun01141322
2009Jun03143124
2009Jun03144401
2009Dec03170001
2009Dec03172339
2010Jan22121924
2008Jun03122912
2010Feb12143618
bash-3.00$

I need to convert month values to numeric format. E.g. May to appear as 05, Aug to appear as 08 etc.

Is there any way of doing this without needing a seperate sed pipe for each month value I need to change to numerical format?

Many thanks
Glyn
# 2  
Old 04-15-2010
If the case of month is not changing then try this. Add substitution for remaining months
Code:
sed "s/Jan/01/;s/Feb/02/;s/Mar/03/"

# 3  
Old 04-15-2010
What does the data look like coming out of the 'ccm query' part of the pipe going into nawk?
# 4  
Old 04-15-2010
Try this, assuming the month is the 4th field of the output of the ccm query:
Code:
ccm query -n cmsynergy_menu.sh -f "%displayname %create_time" | 
nawk 'BEGIN {
  a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  split(a,m,",")
  for(i=1;i<13;i++) {n[m[i]]=sprintf("%02d", i)}
}
{
  gsub(":","")
  $4=n[$4]
}
{print $7 $4 $5 $6}'

# 5  
Old 04-15-2010
Quote:
Originally Posted by rwuerth
What does the data look like coming out of the 'ccm query' part of the pipe going into nawk?
The data comes out like the following:
Code:
 1) cmsynergy_menu.sh-0      Mon May 12 11:47:34 2008
 2) cmsynergy_menu.sh-1      Mon May 12 11:51:52 2008
 3) cmsynergy_menu.sh-10     Tue Aug 19 11:42:42 2008
 4) cmsynergy_menu.sh-11     Mon Aug 25 10:05:40 2008
 5) cmsynergy_menu.sh-12     Tue Aug 26 13:19:37 2008
 6) cmsynergy_menu.sh-13     Tue Sep 02 09:29:06 2008
 7) cmsynergy_menu.sh-14     Thu Sep 25 12:44:12 2008
 8) cmsynergy_menu.sh-15     Mon Sep 29 15:45:54 2008
 9) cmsynergy_menu.sh-16     Thu Oct 02 17:14:59 2008
10) cmsynergy_menu.sh-17     Fri Oct 03 16:45:29 2008
11) cmsynergy_menu.sh-18     Mon Nov 03 16:25:36 2008
12) cmsynergy_menu.sh-19     Tue Nov 11 11:52:03 2008
13) cmsynergy_menu.sh-2      Mon May 19 15:35:02 2008
14) cmsynergy_menu.sh-20     Fri Nov 28 10:24:05 2008
15) cmsynergy_menu.sh-21     Fri Nov 28 16:34:47 2008
16) cmsynergy_menu.sh-22     Tue Dec 02 09:26:13 2008
17) cmsynergy_menu.sh-23     Fri Dec 05 11:00:53 2008
18) cmsynergy_menu.sh-24     Fri Dec 05 11:53:52 2008
19) cmsynergy_menu.sh-25     Mon Jun 01 14:13:22 2009
20) cmsynergy_menu.sh-26     Wed Jun 03 14:31:24 2009
21) cmsynergy_menu.sh-27     Wed Jun 03 14:44:01 2009
22) cmsynergy_menu.sh-27.1.1 Thu Dec 03 17:00:01 2009
23) cmsynergy_menu.sh-28     Thu Dec 03 17:23:39 2009
24) cmsynergy_menu.sh-29     Fri Jan 22 12:19:24 2010
25) cmsynergy_menu.sh-3      Tue Jun 03 12:29:12 2008
26) cmsynergy_menu.sh-30     Fri Feb 12 14:36:18 2010
27) cmsynergy_menu.sh-4      Wed Jun 04 10:43:59 2008
28) cmsynergy_menu.sh-5      Fri Jun 06 09:52:33 2008
29) cmsynergy_menu.sh-6      Mon Jun 16 11:10:34 2008
30) cmsynergy_menu.sh-7      Mon Jul 28 13:49:16 2008
31) cmsynergy_menu.sh-8      Wed Jul 30 16:07:15 2008
32) cmsynergy_menu.sh-9      Wed Aug 13 11:50:44 2008

Anbu33, that's worked fine! Thanks.

Franklin52, I'm going to give that a go as well. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to perform multiple operations on a number before storing to a variable?

(I am using bash) I have a command that will find the line number in a file by searching for a string where it exists. linenumber=$(grep -n "string" $FILENAME | cut -d : -fi) This returns the line number and removes the string. Now that I have the line number I want to subtract 4 from it and... (5 Replies)
Discussion started by: prodigious8
5 Replies

2. UNIX for Dummies Questions & Answers

Multiple substitutions in one expression using sed

Hi, I'm trying to get multiple substitutions in one expression using sed: echo "-foo-_-bar--foo-_bar_-_foo_bar_-foo_-_bar_-" | sed -e "s//-/g" So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash) I have provided bad example. The question is how to use multiple... (6 Replies)
Discussion started by: useretail
6 Replies

3. Shell Programming and Scripting

Multiple Substitutions across Multiple Files

Hey everyone! I am determining the best method to do what the subject of this thread says. I only have pieces to the puzzle right now. Namely this: grep -rl "expression" . | xargs open (I should mention that the intention is to grep through many files containing the "expression" and... (2 Replies)
Discussion started by: Alexander4444
2 Replies

4. Shell Programming and Scripting

loop through files in each folder and perform sed

Dear all, I have few log folders in directory (FILE) and i need to perform sed on each files inside each folders. Here is my script, and i wish to rename each file back to the same file name after modification. Can anyone guide me on my script below? eg: folder1 contain files... (2 Replies)
Discussion started by: ymeyaw
2 Replies

5. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

6. Shell Programming and Scripting

Multiple variable substitutions

Is there anyway to accomplish this? (ksh) FILES_TO_PROCESS='NAME1 NAME2' SOURCE_NAME1=/tmp/myfile TARGET_NAME1=/somewhere/else # other file names for i in $FILES_TO_PROCESS do file1=SOURCE_$i file2=TARGET_$i echo cp ${$file1} ${$file2} <-- how do get this to work. done (2 Replies)
Discussion started by: koondog
2 Replies

7. Shell Programming and Scripting

Return Number of Substitutions made by SED?

Hi guys, Is there any way this can be done, or return whether any substitutions have been made? thanks for any input. skinnygav (using Bash shell) (2 Replies)
Discussion started by: skinnygav
2 Replies

8. Shell Programming and Scripting

Can sed perform editing operations ONLY in the matched region?

Hi: Let's suppose I want to replace all the | by > ONLY when | is between . Usually (and it works) I would do something like sed -e 's/\(\*\)|\(*\]\)/\1>\2/g' where I have to "save" some portions of the matched region and use them with the \n metacharacter. I was wondering if I could... (2 Replies)
Discussion started by: islegmar
2 Replies

9. AIX

Script to perform some actions on multiple files

I have this Korn script that I wrote (with some help) that is run by cron. I basically watches a file system for a specific filename to be uploaded (via FTP), checks to make sure that the file is no longer being uploaded (by checking the files size), then runs a series of other scripts. The... (2 Replies)
Discussion started by: heprox
2 Replies

10. UNIX for Dummies Questions & Answers

Perform a command to multiple files

How do I perform a command to multiple files? For example, I want to look at all files in a directory and print the ones that do not contain a certain string. How do I go about doing this? (4 Replies)
Discussion started by: mcgrawa
4 Replies
Login or Register to Ask a Question