1
http://abc/blah/hhh/25927/3456/bb
http://vfg/blahgg/hhvvh/kkk/25927/2378/bb/mm
http://lah/hhh/25927/fff/bb/somthin

in the above lines, the common number is 25927 always occurring like /25927/ and it is present in each and every line. But the number is a variable I don't know ahead of time, so I cannot use grep 25927; instead it should be something like grep /commonnumber present in all lines/ file

3
  • not sure if possible without a script of some sort.. this might work lines=$(wc -l < ip.txt); for n in $(grep -oE '[0-9]+' ip.txt | sort -u); do cnt=$(grep -c "/$n/" ip.txt); if ((cnt == lines)); then echo "match: $n"; fi ; done
    – Sundeep
    Commented Dec 24, 2016 at 6:55
  • if numbers are unique per line, grep -oE '[0-9]+' ip.txt | sort | uniq -c | grep "^\s*$(wc -l < ip.txt)\s"
    – Sundeep
    Commented Dec 24, 2016 at 6:58
  • a script would be fine.. i will check this. Thanks
    – munish
    Commented Dec 25, 2016 at 9:22

3 Answers 3

2
perl -nE '%a = map { $.==1 || $a{$_} ? ($_,1):()} /(\d+)/g; 
          END{ say keys %a}'   file

Explanation:

  • -n will add a loop over the entire program, like:

    while (<>) { ... }
    
  • -E is needed for perl to execute the command line (inside that loop). It also adds the possibility to use say

  • Every line is passed to /(\d+)/g which matches each number (digits next to each other) separately. Each number if fed into the map.

  • %a is the dictionary of the numbers that appeared in all the lines until now. It is recalculated every line (%a = ...).

  • in the first line $. == 1 all the numbers are stored in the dictionary -- the pair (number , 1) is added; 1 stands for True

  • in the the other lines, all the number are filtered out () unless they were also present in last iteration $a{$_} ?.

  • Finally END{...} prints all numbers that did repeat over all the lines.

2
  • 1
    @Kusalananda - I think I managed to get it (see edit). I believe it works only for part of the question. i.e. it will not find the numbers between lines if none of them is the first line.
    – grochmal
    Commented Jan 5, 2017 at 15:01
  • @grochmal, thank you very much for the help. (1) The map explanation is not perfect: I will try to fix-it. (2) "if none of them is the first line." : then there will be no common number -- answer = {}
    – JJoao
    Commented Jan 5, 2017 at 15:07
1
$ awk -F/ 'NR==1{for(i=1;i<=NF;i++){Arr[$i]++}next}{for(j=1;j<=NF;j++){if ($j in Arr){Arr[$j]++}}}END{for (k in Arr){if(NR==Arr[k]&&k+0!=0){print k,Arr[k]}}}' input.txt

extract the first line with delimter / and store it in array. from second line onwards, check whether the field is there in array. if it's there in array, then increase the value. In the end, check the value against the line number and make sure its number.

0

If you know that it will always be 5 digits in a row delimited by the slash character (as in the path above), you could try something like:

egrep [/][0-9]{5}[/] file

Using egrep allows you to use the extended regular expression syntax if you are using other than the GNU version of grep. See the man page for more information on your version of grep.

2
  • soryr i used the wrong word...should have used number instead of digit
    – munish
    Commented Dec 24, 2016 at 5:03
  • I think you need to give more details on what you're trying to accomplish and what the input data looks like.
    – DDay
    Commented Dec 24, 2016 at 5:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .