Pattern replacing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern replacing
# 8  
Old 07-06-2015
With a standards conforming sed utility you could try:
Code:
sed -e 's/^100\([.]0*\)\{0,1\}%/"&"/g' \
    -e 's/ \(100\([.]0*\)\{0,1\}%\)/ "\1"/g' \
    -e 's/^9[0-9]\([.][0-9]*\)\{0,1\}%/"&"/g' \
    -e 's/ \(9[0-9]\([.][0-9]*\)\{0,1\}%\)/ "\1"/g' file

On a system using a GNU sed utility, you'd have to change that to:
Code:
sed --posix -e 's/^100\([.]0*\)\{0,1\}%/"&"/g' \
    -e 's/ \(100\([.]0*\)\{0,1\}%\)/ "\1"/g' \
    -e 's/^9[0-9]\([.][0-9]*\)\{0,1\}%/"&"/g' \
    -e 's/ \(9[0-9]\([.][0-9]*\)\{0,1\}%\)/ "\1"/g' file

You can flatten these to one-liners by removing the backslashes and <newline>s, but I find them easier to read this way.

If the file named file contains:
Code:
100% 100%
100.00000000% 100.00%
100.0000000000000001% 100.1%
101% 123%
10% 10%
10.0% 10.0%
89.9999999% 89.9%
90% 90%
90.123% 90.987%
99.94% 99.94%
90% 90% 8.98% 92% 193% 96.96%
9.98% 9.98%

it produces the output:
Code:
"100%" "100%"
"100.00000000%" "100.00%"
100.0000000000000001% 100.1%
101% 123%
10% 10%
10.0% 10.0%
89.9999999% 89.9%
"90%" "90%"
"90.123%" "90.987%"
"99.94%" "99.94%"
"90%" "90%" 8.98% "92%" 193% "96.96%"
9.98% 9.98%

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 07-06-2015
Quote:
Originally Posted by ctrld
Aia, your one liner is cool, it works for now. [...] How do I modify this perl statement to take care of that?
Try and see if does what you want.
Code:
perl -pe 's/(?:9\d(?<![0-8]\d)\.\d+|(?<!\S)9\d|(?<!\S)100(\.0+)?)%/"$&"/g' file


Last edited by Aia; 07-06-2015 at 03:08 AM.. Reason: Added 100. support
This User Gave Thanks to Aia For This Post:
# 10  
Old 07-06-2015
Quote:
Originally Posted by Aia
Try and see if does what you want.
Code:
perl -pe 's/(?:9\d(?<![0-8]\d)\.\d+|(?<!\S)9\d|100)%/"$&"/g' file

That doesn't quote either of the following in my sample input file:
Code:
100.00000000% 100.00%

which both seem to meet the stated requirements.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 07-16-2015
Thanks a ton for the suggestions. Its a privilege to interact with experts like you. Thanks Aia and Don.
# 12  
Old 07-16-2015
This awk should do it.
Code:
awk ' { gsub("[0-9]%","&\"",$0); gsub("^[0-9]","\"&",$0); gsub(" [0-9]","\"&",$0); gsub("\" ",FS"\"",$0) }1'

Anything wrong with this ?

Last edited by Peasant; 07-16-2015 at 07:47 AM.. Reason: Misread - Sorry didn't see additional conditions related (>90 etc.)
# 13  
Old 07-16-2015
Quote:
Originally Posted by Peasant
This awk should do it.
Code:
awk ' { gsub("[0-9]%","&\"",$0); gsub("^[0-9]","\"&",$0); gsub(" [0-9]","\"&",$0); gsub("\" ",FS"\"",$0) }1'

Anything wrong with this ?
The request was to quote percentages between 90% and 100% inclusive when the percentage appears at the start of a line or is preceded by a space character. So, with input like:
Code:
Get a loan of 1000 dollars at 5% interest.

no change should be made. But your suggestion changes it to:
Code:
Get a loan of "1000 dollars at "5% "interest.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

2. Shell Programming and Scripting

Replacing a pattern in different cases in different columns with a single pattern

Hi All I am having pipe seperated inputs like Adam|PeteR|Josh|PEter Nick|Rave|Simon|Paul Steve|smith|PETER|Josh Andrew|Daniel|StAlin|peter Rick|PETer|ADam|RAVE i want to repleace all the occurrence of peter (in any case pattern PeteR,PEter,PETER,peter,PETer) with Peter so that output... (5 Replies)
Discussion started by: sudeep.id
5 Replies

3. Shell Programming and Scripting

replacing pattern

hi, I want to do replacing of some pattern by using sed. pattern : " white space / to white space / please help -bhrat (3 Replies)
Discussion started by: bhrat kapoor
3 Replies

4. Shell Programming and Scripting

Replacing pattern

Hi, I have a file which contains the below data. I want to search for a pattern server="http://bushby.mis.amat.com:12440" and remove it from the file. Please let me know how can i do this. <Object name="reverse-proxy-/endeavour/"> ObjectType fn="http-client-config" timeout="1800"... (6 Replies)
Discussion started by: Krrishv
6 Replies

5. Shell Programming and Scripting

Replacing pattern

Hi, I have a file which contains the below data. I want to change pattern to correct format. # tail -1 test.log | awk '{print $8}' 10/09/23 # I want the format to be 23/09/10 (5 Replies)
Discussion started by: nareshkumar522
5 Replies

6. Shell Programming and Scripting

Replacing a pattern using variable?

ip1="xxx" ip2="bbb" sed 's/$ip1/$ip2/g' (3 Replies)
Discussion started by: shivarajM
3 Replies

7. UNIX for Dummies Questions & Answers

Replacing between pattern

Hi all, I been searching the forum for a few hours now and can't find exactly what I need to replace text between two patterns. Below is what I want to accomplish /* Any text between these to sympols */ changed to.... /* This will be the new text to change */ ..and I'm... (3 Replies)
Discussion started by: Pauky
3 Replies

8. Shell Programming and Scripting

Replacing all except for the matching pattern

Hi all I need to replace all characters in a file except for the matching pattern eg. I need to replace all character with '*' except for the pattern "abc" Input "sdfhgsdf abc ##%$#abcsdfh sdfjkfff" Output "******abc******abc*************" Request for single liner solution (1 Reply)
Discussion started by: raghav288
1 Replies

9. Shell Programming and Scripting

replacing a pattern in a file

Hi guys, i have a pattern that i am searching in a file and i want to extract some of this pattern ... module TS1N65ULPA96X32M4 ( .... i want to extract only TS1N65ULPA96X32M4 part and i do the following sed 's/module \(x*\).*/\1/' name_of_file but this is not quite right. could... (6 Replies)
Discussion started by: ROOZ
6 Replies

10. Shell Programming and Scripting

Replacing pattern in variable

My String variable is holding value as - abc"def I want to replce " with \" I tried with awk : echo $var | awk '{gsub(/"/,"\"");print}' and I am getting an error, `)' is not expected. (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question