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
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.
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.
Summary: Well, I think you could do something like this..( the earlier one will not give the result as the file1 and file2 are not read in order). grep -v "summed" file1 > file3 [removed the first line from fi...
Summary: Hi, I want to read a file in a C shell script and process all the words of each line as a command set. something like this read line from file get the first word from line and assign it to a var...