0

I am passing a text file as a command line input to a script and need to parse through the file searching for a specific pattern:

00 TOOL     | Running /variable/directory/path/to/the/tool/executable in batch (pid xxxxx)

Now I have to extract /variable/directory/path/to/the/tool/executable to a variable say $executable.

Here, the portion which remains constant is 00 TOOL | Running at the starting of a line and in batch (pid xxxxx) at the ending of a line where xxxxx is again a variable.

Provided that in the input text file, this line will appear only once.

I have found out the snippet to read line by line, but can not find out a way to read the above said variable:

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
done < "$1"

Any help will be really appreciated!

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
RandomCoder
  • 79
  • 1
  • 7

1 Answers1

2

In case your path's value is not a set field and you want to match it as regex form then try following once.

val=$(awk 'match($0,/\/[^ ]*/){print substr($0,RSTART,RLENGTH);exit}' Input_file)

You could set its value to a variable by doing var=$(awk code above)

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • I need only the first line of the output. I tried `var=$(awk 'match($0,/\/[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file | head -n 1)`. As the `Input_file` is of big size, its taking loads of time to get output! Any alternative suggestion would be helpful!. – RandomCoder Sep 30 '18 at 18:30
  • @RandomCoder, if you need only very first match then could you please try my edited solution and let me know if that helps you? – RavinderSingh13 Sep 30 '18 at 18:32
  • Yes. I only need the first match! – RandomCoder Sep 30 '18 at 18:35
  • @RandomCoder, then please try my edited one it shouldn't not take that much time also since it will exit after first match, let me know how it goes then? – RavinderSingh13 Sep 30 '18 at 18:35