I have a file named full_path.txt where it has a few hundred lines of directory paths.Right now I'm able to go to any path by modifying the shell script i wrote and grep what i need. i find it tedious to go to the code, change it, save it and then run it again to get to the desired line and grep what i want.

I want to know if there is a way to add something to the end of the script so that it automatically takes me to that line and greps.

I know the thing I have to do is change the FNR to whichever line i want, but is there a way to do it while I run it?
for example if i want to go to line 15, i can just type gotoline.sh 15

This is the code i have so far

line=$(awk 'FNR==1' full_path.txt) 
cd "${line}" 
grep -a "RESULT:" console_debug.log > Result.txt

Awk gives the ability to pass input arguments/variables to a script (via -v option):


line=$(awk -v l_num=$1 'NR == l_num' full_path.txt)

With the approach above you can pass the needed argument when running main shell script:

gotoline.sh 15

where 15 will be passed as shell script argument $1

  • @VineethThomas, you're welcome – RomanPerekhrest Dec 22 '18 at 16:31
  • 1
    You could also do line=$(sed -n "${1}p" full_path.txt) if you like brevity. – glenn jackman Dec 22 '18 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.