Skip to main content
added 29 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

UPDATE: Found 1 brute force solution & itsit's working only for range.

        count=0;
        num1=348;
        for i in `sed 's/\([0-9]\+\-[0-9]\+\)/:&:/g' testme.txt  | 
    awk -F: '{ for(i=1; i<=NF; i++) if($i ~/[0-9]+-[0-9]+/){print $i} }'`;       
do 
    lh=`echo $i | awk -F\- '{print $1}'`; 
    rh=`echo $i | awk -F\- '{print $2}'`;  
    if [ $lh -le $num1 -a $rh -ge $num1 ]; 
    then  count=`expr 
 $count + 1`; fi; done

   count=`expr $count + 1`; 
    fi; 
done
echo $count;

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

UPDATE: Found 1 brute force solution & its working only for range.

        count=0;
        num1=348;
        for i in `sed 's/\([0-9]\+\-[0-9]\+\)/:&:/g' testme.txt  | awk -F: '{ for(i=1; i<=NF; i++) if($i ~/[0-9]+-[0-9]+/){print $i} }'`; do lh=`echo $i | awk -F\- '{print $1}'`; rh=`echo $i | awk -F\- '{print $2}'`;  if [ $lh -le $num1 -a $rh -ge $num1 ]; then  count=`expr $count + 1`; fi; done

         echo $count;

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

200,300,234,340-350,400,360,333-339
409-420
4444-31231231
348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

UPDATE: Found 1 brute force solution & it's working only for range.

count=0;
num1=348;
for i in `sed 's/\([0-9]\+\-[0-9]\+\)/:&:/g' testme.txt  | 
    awk -F: '{ for(i=1; i<=NF; i++) if($i ~/[0-9]+-[0-9]+/){print $i} }'`;       
do 
    lh=`echo $i | awk -F\- '{print $1}'`; 
    rh=`echo $i | awk -F\- '{print $2}'`;  
    if [ $lh -le $num1 -a $rh -ge $num1 ]; 
    then   
        count=`expr $count + 1`; 
    fi; 
done
echo $count;
edited body
Source Link
Tingrammer
  • 490
  • 1
  • 3
  • 8

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

UPDATE: Found 1 brute force solution & its working only for range.

        count=0;
        num1=348;
        for i in `sed 's/\([0-9]\+\-[0-9]\+\)/:&:/g' testme.txt  | awk -F: '{ for(i=1; i<=NF; i++) if($i ~/[0-9]+-[0-9]+/){print $i} }'`; do lh=`echo $i | awk -F\- '{print $1}'`; rh=`echo $i | awk -F\- '{print $2}'`;  if [ $lh -le $num1 -a $rh -ge $num1 ]; then  count=`expr $count + 1`; fi; done

         echo $count;

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

UPDATE: Found 1 brute force solution & its working only for range.

        count=0;
        num1=348;
        for i in `sed 's/\([0-9]\+\-[0-9]\+\)/:&:/g' testme.txt  | awk -F: '{ for(i=1; i<=NF; i++) if($i ~/[0-9]+-[0-9]+/){print $i} }'`; do lh=`echo $i | awk -F\- '{print $1}'`; rh=`echo $i | awk -F\- '{print $2}'`;  if [ $lh -le $num1 -a $rh -ge $num1 ]; then  count=`expr $count + 1`; fi; done

         echo $count;
edited body
Source Link
Tingrammer
  • 490
  • 1
  • 3
  • 8

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400;360400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400;360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like,

        200,300,234,340-350,400,360,333-339
        409-420
        4444-31231231
        348

I want to find out whether number 348 is present or not. 348 is present in 2 places:

  • 340-350
  • In last line.

How to find it?. I tried using regex in sed,awk, but I am not able to completely write the script to capture the number range. Is there any other way to find it?

added 8 characters in body
Source Link
cuonglm
  • 158.2k
  • 41
  • 342
  • 420
Loading
deleted 8 characters in body; edited title
Source Link
Faheem Mitha
  • 36.1k
  • 33
  • 129
  • 190
Loading
Source Link
Tingrammer
  • 490
  • 1
  • 3
  • 8
Loading