Using sed to replace a range of number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed to replace a range of number
# 1  
Old 10-09-2014
Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers
Code:
3 
26 
20 
5

. For the numbers that are greater than zero and less than 25, SED would add the word range after the number. Output would look something like this:
Code:
 3 range 
26 
20 range
5 range

. Have tried variations of this:
Code:
sed 's/[0-25]/range/g' file

. Can't get this logic to work. Any help would be appreciated.
# 2  
Old 10-09-2014
Quote:
Originally Posted by jimmyf
Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers
Code:
3 
26 
20 
5

. For the numbers that are greater than zero and less than 25, SED would add the word range after the number. Output would look something like this:
Code:
 3 range 
26 
20 range
5 range

. Have tried variations of this:
Code:
sed 's/[0-25]/range/g' file

. Can't get this logic to work. Any help would be appreciated.
Hi jimmyf,

Following awk may help you in same.

Code:
awk '($1+0 <= 25) {print $1 OFS "Range";next} 1'  Input_file

Output will be as follows.
Code:
3 Range
26
20 Range
5 Range

EDIT: Above code will take 0 too, to avoid taking 0 use following.
Code:
awk '($1+0 >0 && $1+0 <= 25) {print $1 OFS "Range";next} 1' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-09-2014 at 02:42 AM.. Reason: Edited code to add word range in it+ 1 more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-09-2014
/[0-25]/ means 0-2 or 5, so it matches any line with one of the numbers: 0,1,2 or 5 (BTW In your requirements you say greater than 0, but your example would also match 0)

To match the numbers 1-25, you could use: /[1-9][0-5]\{0,1\}/

But to avoid matching "125" of example you need to anchor it front and back, for example:
/^ *[1-9][0-5]\{0,1\} /
Notice the trailing space ...

Another approach is not to use regex, but a numerical comparison. But for this you cannot use sed, which is a streamline editor. Instead you could use awk for example or another scripting language...
Code:
awk '$1>0 && $1 <=25' file


Last edited by Scrutinizer; 10-09-2014 at 02:28 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 10-09-2014
That's not the desired output.
# 5  
Old 10-09-2014
Hi jimmyf,

I just now edited my code in post#2, kindly refer the same.

Thanks,
R. Singh
# 6  
Old 10-09-2014
Scrutinizer, your logic works nicely but is there a way to also echo the original numeric value?

---------- Post updated at 01:34 AM ---------- Previous update was at 01:27 AM ----------

Thank you very much guys.
# 7  
Old 10-09-2014
Quote:
Originally Posted by jimmyf
Scrutinizer, your logic works nicely but is there a way to also echo the original numeric value?

---------- Post updated at 01:34 AM ---------- Previous update was at 01:27 AM ----------

Thank you very much guys.
You lost me. No numbers are being changed. Why do you want to echo the original number and echo the original number plus the word range?

If you want to use sed instead of awk for this, try:
Code:
sed -e 's/^ *[1-9] *$/& range/' -e 's/^ *1[0-9] *$/& range/' -e 's/^ *2[0-5] *$/& range/' file

Note that using:
Code:
sed 's/^ *[1-9][0-5]\{0,1\} /&range/' file

will not match any line that does not have a trailing space, and, for lines that do have a trailing space:
  1. will not match lines with the values 16 through 19 (which are in range),
  2. will match lines with the values 30 through 35 (which are not in range),
  3. will match lines with the values 40 through 45 (which are not in range),
  4. will match lines with the values 50 through 55 (which are not in range),
  5. will match lines with the values 60 through 65 (which are not in range),
  6. will match lines with the values 70 through 75 (which are not in range),
  7. will match lines with the values 80 through 85 (which are not in range), and
  8. will match lines with the values 90 through 95 (which are not in range).
These 3 Users Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

2. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

3. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

4. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

5. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

Replace a null from grep to a number 0 using sed

Hi I have a file which contains count for a code. Code is first field and count is second field. I am trying to search the code and get correspond count. File look like this. temp.out A 10 B 20 I am searching for C , if C is not there I will have get value 0. I have... (5 Replies)
Discussion started by: dgmm
5 Replies

7. Shell Programming and Scripting

Using SED to replace a random number?

Hi all, I need to run a number of scripts which have a certain phrase on them so I have identified these by; grep -l 100 *script | sort -u Normally I could just run something along the lines of; for i in `grep -l 100 *script | sort -u`; do ./${i}; done However before I run each of... (0 Replies)
Discussion started by: JayC89
0 Replies

8. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

9. Shell Programming and Scripting

How to replace a range of text with sed or awk?

Howdy! I'm trying to automate editing of a configuration file (custom.conf for GDM). I need to find every line between a line that starts with "" and the next line that starts with "", I want to preserve that line, but then delete all the lines in that configuration section and then insert... (3 Replies)
Discussion started by: TXTad
3 Replies

10. Shell Programming and Scripting

Sed command to find, manipulate and replace a number

Hi, Im very new to the world of sed so I'm really not even sure if this is possible. What i need to do is read from a flat file and every time i see this line: VAL=123,456 I need to change 456 to 457 for every occurence of this line in the file. The numbers 123 and 456 are different for... (6 Replies)
Discussion started by: LT_2008
6 Replies
Login or Register to Ask a Question