0

Suppose i have one line Script : Script Name is script1.sh has below line on it -

# sh script.sh #

So how can i take only script.sh name from script1.sh.

What I have done is below but that is not fully fruitful to me get the exact output that I want.

while read line
do

    called_script= awk -F ':' '{print $1 }' final_calling_script_name

    qwe= grep '*.sh' $called_script

    echo $called_script " :  $qwe"

    done<'file_that_contains_data_of_script1_line_by_line'

Can anybody help me?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Indrajeet Gour
  • 4,020
  • 5
  • 43
  • 70
  • 3
    Can you state the problem more clearly? I think you are saying: "Given the name of a file, how do I extract a specific word from a specific line that meets a specific pattern"? Or is it always "the second word from the first line"? Or… your code example is not really making this any clearer. Can you try to be more specific? – Floris Nov 29 '13 at 15:44
  • i want specific pattern of word for that line. like that if i want the word(which has pattern "*.sh") from given specific line. what should i do??? – Indrajeet Gour Dec 03 '13 at 10:58
  • I think you want to "match a _word_ that matches a pattern based on the contents of a file". If so, then see http://stackoverflow.com/questions/1546711/can-grep-show-only-words-that-match-search-pattern – Floris Dec 03 '13 at 14:43

2 Answers2

1

If what you want here is basically "the second word" you can use "cut"

echo "sh script.sh" | cut -d ' ' -f 2

The -d ' ' tells cut that the "delimiting character" is a space, the -f 2 tells cut that you want column number 2.

DusteD
  • 1,400
  • 10
  • 14
0
echo "sh script.sh" | { read a b; echo "$b"; }

EDIT:

After you've clarified your requirements in the notes below, I would propose this command:

echo "script1.ksh script2.pig script3.sh" | grep -oe '\w*\.sh'
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • but i do have some different scenario : where i have to search like pattern (i.e. '*.txt') if it will encounter on that line at any place it is not specific to any column, but i have to decide at run time ; to that specific pattern. – Indrajeet Gour Nov 29 '13 at 15:35
  • There's a big gotcha here: `$a` and `$b` will only exist inside the pipeline, inside the curly braces. – John Kugelman Nov 29 '13 at 15:41
  • That's right, @JohnKugelman, therefore the braces. But you can use the output of that line for further processing. Or you could use `read a b < <(echo "sh script.sh")` if you want to continue using the variables instead. – Alfe Dec 02 '13 at 08:43
  • I'm sorry, @user2987780, but I did not understand that description of your problem. Do you need to find a specific line _in_ a given file and then process that line? – Alfe Dec 02 '13 at 08:45
  • @Alfe : example - script1.ksh script2.pig script3.sh (that is the line and what i want to search for is the pattern '*.sh' from this line, and the output that i requires is "script3.sh" ) [and *Note - position of the script will vary in future as well ] – Indrajeet Gour Dec 03 '13 at 12:28
  • I've updated my answer according to your requirements. – Alfe Dec 03 '13 at 14:46
  • Hey Thanks a lot #Alfe u have solve this problem. – Indrajeet Gour Dec 08 '13 at 15:06