Print line number from piped output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print line number from piped output
# 1  
Old 05-23-2016
Print line number from piped output

i need to do something like this:

script.sh
Code:
#!/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:

Code:
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:
Code:
sed: can't read sh: No such file or directory

# 2  
Old 05-23-2016
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:
Code:
#!/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?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-23-2016
sorry i should have provided this bit of information.

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

Code:
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.
# 4  
Old 05-23-2016
Hello SkySmart,

You could use following and let me know how it goes then.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 05-23-2016
I'm afraid you're out of luck. When catted, 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.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-23-2016
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:
Code:
#!/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:
Code:
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:
Code:
./tester

or:
Code:
/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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Can't Output Piped Perl In-line command to a File

Hello, I'm pretty stumped, and I don't know why I am not able to redirect the output to the 'graphme' file with the command below in Fedora 18. tcpdump -l -n -t "tcp == 18" | perl -ane '($s,$j)=split(/,/,$F,2); print "$s\n";' > graphme In case you're wondering, I was following the example... (2 Replies)
Discussion started by: ConcealedKnight
2 Replies

5. Shell Programming and Scripting

Print line number

how i need help again i have file input.txt like this 48:20111008_20111122:31231|123| 78:20111008_20111122:435453|321112| where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file, i am using manual awk for get that file like this awk 'NR==48... (6 Replies)
Discussion started by: zvtral
6 Replies

6. Shell Programming and Scripting

print the number of fields in each line

I am looking for equivalent of following awk command in perl # awk '{ print NF ":" $0 } ' junk1 8:VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP DESCR 8:2 NIC6 cont 34 y n shal_orgrp /shal 8:4 NIC5 signa 52 n y shal_orgrp... (3 Replies)
Discussion started by: dynamax
3 Replies

7. Shell Programming and Scripting

print number of words in each line

Hi, Please suggest a way to print number of words in the end of each line. <input file> red aunt house blue sky bat and ball game <output file> red aunt house 3 blue sky 2 bat and ball game 4 Thanks! (2 Replies)
Discussion started by: mira
2 Replies

8. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

9. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

10. Shell Programming and Scripting

print first number in each line

I have a file such as: .....12345......67890...xxx ....123456....78901...yyy ...1234567...89012...zzz ..12345678.90123...aaa Where the '.' character is a SPACE. I'm trying to print just the first number in each line. such as: 12345 123456 1234567 12345678 Both the number of... (1 Reply)
Discussion started by: dcfargo
1 Replies
Login or Register to Ask a Question