Computing.Net > Forums > Unix > print lines from file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

print lines from file

Reply to Message Icon

Name: Nick
Date: September 22, 2002 at 07:22:41 Pacific
OS: mS
CPU/Ram: adfaf
Comment:

Hi,
I am new to shell programming and I need your help.

I just learn that I can use the sed comand to print certain lines of an input file.
e.g. sed '1,3p' will print out line 1 to 3.

I have a shell script (print.sh) that contains the following command:

sed '$1,$2p'

When I try to do print.sh 1 3,
I got error message "sed command garbled". I am trying to feed in variabled and I must have done something wrong, please advise.




Sponsored Link
Ads by Google

Response Number 1
Name: jimbo
Date: September 23, 2002 at 07:52:43 Pacific
Reply:

A few things:

Use double quotes when trying to perform variable substition w/ sed. Also the variables need to be enclosed from the rest of your statement w/ {}. Last, if you only want to show the lines you are printing and not duplicates of those lines w/ the rest of the file, use the -n option to suppress output. here is what your command should look like with that being "sed":

sed -n "${1},${2}p"

You might want to use different variable names besides 1 and 2 also. They have special meanings (positional parameters) to the shell you run the script in.

-jim


0

Response Number 2
Name: Nick
Date: September 23, 2002 at 08:06:11 Pacific
Reply:

Hi Jim,

It works, thanks a million.


0

Response Number 3
Name: jimbo
Date: September 23, 2002 at 08:06:19 Pacific
Reply:

the 1 and 2 variables work fine. did not read the last part of your post.

-jim


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: print lines from file

print columns from files to a file www.computing.net/answers/unix/print-columns-from-files-to-a-file/7615.html

Process line from file -Cshell scr www.computing.net/answers/unix/process-line-from-file-cshell-scr/5072.html

extract a specific line from file www.computing.net/answers/unix/extract-a-specific-line-from-file/5381.html