0

I want to find a specific line in a log file that contains a string and then writes that line to an array. I'm brand new to this scripting language so I having a difficult time finding the exact answer I need. Here is what I have so far. It's the next step I can't find. Writing the array.

tail -f foo.log | grep --line-buffered "foo"

So say the file contains the following lines:

1516049080,1,item0,0,end

1516049080,item1,item2,0,id,foo:12345678

1516049080,item3,item4,0,files-count,0

I want to find the value foo:12345678 and save that to a variable. The value 12345678 was generated in the log file and at the time of retrieval is not known. The id would, however, always be prefixed by foo:.

Thanks in advance.

Rhonda

Rhonda
  • 985
  • 5
  • 18
  • 32
  • Possible duplicate: https://stackoverflow.com/questions/971162/how-to-initialize-a-bash-array-with-output-piped-from-another-command – Jerry Jeremiah Jan 15 '18 at 23:04
  • ARRAY=(\`tail -f foo.log | grep --line-buffered "foo"\`) – Jerry Jeremiah Jan 15 '18 at 23:06
  • @JerryJeremiah what if the line contains spaces :) You need to use quotes. – PesaThe Jan 15 '18 at 23:09
  • @PesaThe True enough. Maybe it isn't a duplicate then... – Jerry Jeremiah Jan 15 '18 at 23:13
  • By the way, if you have just **one** line, why do you want to store it in an array? Perhaps you want to store **all** lines containing your string to the array? – PesaThe Jan 15 '18 at 23:16
  • I think I need to get the entire line that contains the value "foo". I don't really want the full file. It's huge. Ultimately I am trying to find an id value that was generated and added to the log file. At the time I am trying to get it I don't know what it is. I only know what other values may be around it. The full line would look something like this 1516049080,item1,item2,0,id,foo:12345678 and I want to retrieve foo:12345678 where foo is the only value I know. Make sense? – Rhonda Jan 15 '18 at 23:18
  • @Rhonda maybe update the question with input/desired output. I am just saying array is kind of pointless if you have just one element in it. You can just use ordinary variable. – PesaThe Jan 15 '18 at 23:20
  • I was thinking array because the line is a comma delimited string. – Rhonda Jan 15 '18 at 23:33
  • @Rhonda What about some extreme cases? If it's ensured that there is always only one string "foo" and the number is directly after that to the end of the line, simple `var=$(tail foo.log | grep -o "foo.*")` should do. – PesaThe Jan 15 '18 at 23:36
  • It was an example I found on another website. I wasn't sure if I need line-buffered or not. – Rhonda Jan 15 '18 at 23:38
  • @Rhonda also note that tail by default prints only 10 last lines of your log. I don't think you want to do that. Try this and let me know if it does what you want: `var=$(grep -o "foo.*" foo.log)`. – PesaThe Jan 15 '18 at 23:43

1 Answers1

1

If I understood correctly for example you have a logfile 'logfile.log':

$ cat logfile.log
something else
1516049080,item1,item2,0,id,foo:12345678
something else
7777777777,item3,item4,0,id,foo:87654321

If you need array with values: [12345678, 87654321] you can just do this:

$ array=(`cat logfile.log | grep foo | awk -F":" '{print $2}'`)
$ echo ${array[*]}
12345678 87654321
$ echo ${array[0]}
12345678
$ echo ${array[1]}
87654321
Konstantin Vustin
  • 6,521
  • 2
  • 16
  • 32
  • Get rid of `cat` and `grep` altogether, `array=($(awk -F":" '/foo/ {print $2}' file))` (avoid using *backticks*, `$(...)` is much more readable) – David C. Rankin Jan 16 '18 at 07:16
  • Absolutely agree, exept `$(...)`. I think _backticks_ are looking better – Konstantin Vustin Jan 16 '18 at 07:23
  • It's a matter of choice. backticks are not deprecated. It's just a general recommendation (and especially when you try and include them in a comment) You have a good solution, but you are spawning two more subshells than necessary using `cat .. | grep ... | awk` where only `awk` is needed. Also, if it turns out he wants `foo:` attached (it is unclear), you may need `awk -F":" '/foo/ {printf "foo:%d\n", $2}' file` – David C. Rankin Jan 16 '18 at 07:25
  • Your solution is perfect. But the topicstarter said: " *I'm brand new* to this scripting language", i thinked solution with _grep_ would be easier to understand just because topicstarter knows _grep_ – Konstantin Vustin Jan 16 '18 at 07:43
  • I can't argue with that. Only to say when answering questions on StackOverflow, you step into the roll of *teacher*. In doing that it is good to point out the best-practices. You can always provide alternative examples. But like I said to begin with, you have a good solution, but to use best-practices, it could be tweaked just ever so slightly. – David C. Rankin Jan 16 '18 at 08:04