0

I have a file with numerical data, and reading the variables from another file extract the correct string.

I have my code to read in the variables.
The problem is the variable can occur at different points within the string, i only want the string that has the variable on the right-hand side, i.e. the last 8 characters.
e.g.

grep 0335439 foobar.txt

00032394850033543984  
00043245845003354390  
00060224460033543907 
00047444423700335439 

In this case its the last line.

I have tried to write something using ${str: -8}, but then I lose the data in front.

I have found this command

grep -Eo '^.{12}(0335439)' foobar.txt

This works, however when I use my script and put a variable in the place it doesn't, grep -Eo '^.{12}($string)' foobar.txt.

I have tried without brackets but it still does not work.


Update:

In this case the length of the string is always 20 characters, so counting from the LHS is OK in my case, but you are correct its was not the answer to the original question. I tried to comment the code so say this but pasting it into the comment box removed the formatting.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Red_badger
  • 119
  • 1
  • 15
  • 5
    So you want `grep '0335439$' foobar.txt`? – Wiktor Stribiżew Nov 10 '21 at 14:12
  • no, that was just my grep command example. – Red_badger Nov 10 '21 at 14:35
  • I have found this command grep -Eo '^.{12}(0335439)' foobar.txt this works, however when i use my script and put a variable in the place it doesnt,
    grep -Eo '^.{12}($string)' foobar.txt i have tried without brackets but it still does not work.
    – Red_badger Nov 10 '21 at 14:35
  • 3
    Of course, you need to use double quotes , `grep -Eo "^.{12}$string" foobar.txt`, otherwise, the variable expansion does not take place. – Wiktor Stribiżew Nov 10 '21 at 14:40
  • Thanks, had issues with editing the reply – Red_badger Nov 10 '21 at 14:43
  • 2
    See: [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – John Kugelman Nov 10 '21 at 14:44
  • 1
    Here is full code with both solutions ` #!/bin/bash set -x while read id; do echo "$id" >> id.txt ##n Search on the string ## sentry=`grep $id /foo/largedata.txt` ## match the variable from position 12 in the string ## sentry=`grep -Eo "^.{12}$id" /foo/largedata.txt` ## match the variable, using awk this time - took longer than the grep -Eo command sentry=`awk -v n=8 -v kw="$id" 'substr($0, length()-n, n) == kw' /foo/largedata.txt` echo $sentry >> vespa_subidawk.txt done – Red_badger Nov 10 '21 at 15:05
  • Not sure dupe target is correct considering the problem statement of `i only want the string that has the variable on the right-hand side, i.e. the last 8 characters`. `"^.{12}$id"` pattern is incorrect because it assumes that there are always 20 characters in each line. – anubhava Nov 10 '21 at 15:09
  • 1
    @anubhava, In this case the length of the string is always 20 characters, so counting from the LHS is OK in my case, but you are correct its was not the answer to the original question. I tried to comment the code so say this but pasting it into the comment box removed the formatting. – Red_badger Nov 11 '21 at 15:10
  • Thanks @Red_badger better we put this important comment in your question – anubhava Nov 11 '21 at 16:41

1 Answers1

3

i only want the string that has the variable on the right-hand side, i.e. the last 8 characters

A non-regex approach using awk is better suited for this job:

s='00335439'
awk -v n=8 -v kw="$s" 'substr($0, length()-n, n) == kw' file

00043245845003354390

Here we passing n=8 to awk and using substr($0, length()-n, n) we are getting last n characters in a line, which is then compared against variable kw which is set to a value on command line.

anubhava
  • 761,203
  • 64
  • 569
  • 643