Help in simplifying a sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in simplifying a sed command
# 1  
Old 03-03-2005
Help in simplifying a sed command

I need to do a substitution: CPF to ,C,P,F, CPM to ,C,P,M, SPF to ,S,P,F etc. I can do each of them with separate substitutions e.g. s/CPF/,C,P,F/ but I'd like to know if there is a more elegant solution. In general, how can I use the results of the search in the substitution, something like s/A.B/,A,.,B,/
# 2  
Old 03-03-2005
assuming the pattern is the only pattern on line.
sed -e 's#\(.\)\(.\)\(.\)#,\1,\2,\3#' file
# 3  
Old 03-03-2005
Thank you.
# 4  
Old 03-04-2005
You can try also as,

Code:
# echo "CPF CPM SPF" | sed -e 's%\([CS]\)P\([FM]\)%,\1,P,\2%g'
,C,P,F ,C,P,M ,S,P,F

HTH.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Simplifying awk/sed short pipeline

I have a file like this: FileName,Well,Sample Description,Size ,Calibrated Conc. ,Assigned Conc. ,Peak Molarity ,Area,% Integrated Area,Peak Comment,Observations 2017-11-15 - 13.49.50.D1000,EL1,Electronic Ladder,25,5.22,,321,0.803,,,Lower Marker 2017-11-15 - 13.49.50.D1000,EL1,Electronic... (6 Replies)
Discussion started by: Xterra
6 Replies

2. Shell Programming and Scripting

Simplifying sed/tr

Hi all, I don't have much experience with shell scripting and I was wondering if there's a shorter way to write this. Basically, given a list of strings separated by new lines, I want to prepend each string with a prefix and separate the strings with commas i.e. stra strb strc becomes... (3 Replies)
Discussion started by: vshan
3 Replies

3. Shell Programming and Scripting

need help simplifying an if statement

the code below is a small fragment of the actual line, in fact i have about 20 values i'm comparing and want to know if it can be simplified. other than the x.xx.xx format of the value they have nothing in common if || || ; then do this else do this fiany suggestions? (6 Replies)
Discussion started by: crimso
6 Replies

4. Shell Programming and Scripting

Simplifying IPv6 addresses from user input

hi friends, im new to scripting and am tiring out to valid an ip address if user inputs 9 : 003 : 0 : 0 : 100 : 256 ,i need to suppress all leading zeros and single zero ie out put for the same should be 9 : 3 : : : 100 : 256 can any help using awk ,regular expression to split the string... (1 Reply)
Discussion started by: sandeepjeede
1 Replies

5. UNIX for Advanced & Expert Users

Simplifying my script

Hi, Is there a way to simplify the below script? Because I am having problems executing this if I added this to CRON. Also, you may notice that its objective is to put all information in one file (rm1.txt). And in addition file "sRMR_6.txt" to sRMR_23.txt" changes its information everyday.... (4 Replies)
Discussion started by: vibora
4 Replies

6. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

7. Shell Programming and Scripting

simplifying awk

tcpdump -nr testdump|awk '!/:/;gsub(/^+|+$/,""){print $3};a!~$0;{a=$0};{print $3};!/length/;/./;!/11\:/;!/8 7 6 5 4 3 2 1/;!/UDP/{b=$0} END {for (j=i-1; j>=0; ) print b };{for (i=NF; i>0; i--) printf("%s ",i);printf ("\n")}' *NOTE IN j>=0 the ; ) was given a space since a smiley is showing up...... (1 Reply)
Discussion started by: sil
1 Replies

8. Shell Programming and Scripting

Simplifying the For loop

Is there a way to simplify stating 1 through to 10, for example in a for loop construct? for x in 1 2 3 4 5 6 7 8 9 10 do .... done I have tried (1-10) with no luck.. thanks (2 Replies)
Discussion started by: sirtrancealot
2 Replies
Login or Register to Ask a Question