Print line number from piped output

i need to do something like this:

script.sh

#!/bin/sh

echo "hello"
echo "My First name is John"
echo "My Last name is Smith"
echo "I am here to save you a lot of work"

sed -n 4,5p $0

i dont want to run the script. i just want to pull out specific line from it. so the logic here is i'm pulling out specific lines from a running script.

when i run script.sh, i only want the output to be:

echo "My Last name is Smith"
echo "I am here to save you a lot of work"

again, logic is, $0 is the script that's running. i want to output specific lines from $0.

i'm getting the following error:

sed: can't read sh: No such file or directory

What operating system are you using?

Where is your script located?

Exactly what command line do you use to invoke your script?

If you change your script to:

#!/bin/sh
printf '%s\n' "$0"
echo "hello"
echo "My First name is John"
echo "My Last name is Smith"
echo "I am here to save you a lot of work"

sed -n 4,5p "$0"

and run it again, exactly what output do you get?

1 Like

sorry i should have provided this bit of information.

i want the script to work when it is run this way:

cat script.sh | sh

so when i run the above, it should only print out line 4 and 5.

this to be used on all OSes (linux/aix/hpux/sunos). i'd like to do this without having to modify the script.sh itself.

Hello SkySmart,

You could use following and let me know how it goes then.

echo -e "hello\nMy First name is John\nMy Last name is Smith\nI am here to save you a lot of work" | sed -n '3,4p'

Also in your script as you haven't mentioned any Input stream for sed so it couldn't read the lines which you have printed as they are getting printed to standard output(our monitor). So off course it will produce an error. Also couldn't see there are 4th and 5th lines there into your Input shown, so I have changed it to 3rd and 4th lines in solution, you could change accordingly. I hope this helps.

Thanks,
R. Singh

1 Like

I'm afraid you're out of luck. When cat ted, the file name is lost, and sh becomes $0. Supplying sh 's full path wouldn't help either - you'd have gotten rid of the error message but analyse a binary file instead, with dubious results.
But even when running the script, you'd need to take extra action NOT to echo those lines to stdout but have sed print to stdout.

1 Like

Note that if you run the script instead of piping the contents of the script into sh , you might get what you want...

If a file named tester contains:

#!/bin/sh
printf '%s\n' "$0"
echo "hello"
echo "My First name is John"
echo "My Last name is Smith"
echo "I am here to save you a lot of work"

sed -n 4,5p $0

and you make that file executable and include its location in $PATH and invoke it with tester ; or invoke it with ./tester you'll get output similar to:

tester
hello
My First name is John
My Last name is Smith
I am here to save you a lot of work
echo "My First name is John"
echo "My Last name is Smith"

where the 1st line of the output will change depending on the way you invoked it and could be something like:

./tester

or:

/Users/dwc/test/unix.com/ksh/Print_line_number_from_piped_output/tester

And even if you haven't made it executable, if you invoke it with sh /path/to/tester or sh tester , you'll still get output similar to the above.

1 Like