Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Chunks" of text by NR.

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I have a small problem: the following code does not
do what I think it should.

echo "12 115" | awk ' {
all = $ARGV[1]
split(all, new_all, " ")
SR = new_all[1]
ER = new_all[2]
while((getline buf < &quot;README&quot;) > 0) {
for (x=buf ;x <= NR ; x++)

Here is where I get stuck.
What I want to be able to do is read the text between
SR and ER.
There must be a better way.

I know sed -n '12 , 115'p would do better but I have
forgotten how to get shell vars(that is what the two values
represent) into a sed expression???

Thanks
 
Nevermind.

sed -n &quot;$start , $end&quot;p file


 
Hi marsd-

This should work:

awk 'NR == 12 , NR == 115 { print }' infile

HTH


flogrr
flogr@yahoo.com

 
Hi, Flogrr.

I should have explained a little better.
What I was trying to do was get two shell vars

echo -n &quot; val one please: &quot;
read val_1
echo -n &quot;val two please: &quot;
read val_2

like that..
and then

echo &quot;$val_1 $val_2&quot; | awk '
all = $ARGV[1]
split(all , n_all, RS)
SR = n_all[1]
ER = n_all[2]

This works fine, i have the values I want in discrete vars.
The problem now is to apply these values to NR ranges in
a file.
I tried this:
while((getline buf < &quot;filename&quot;) > 0 ) {
for(x=buf ; x <= NR ; x++)

and you can probably already see my problem.
It never quite works.

To test:
If you issue ; print $x , you get something
like 111 22 which is not going to give me what I want.
Print x gives me the predictable file content result.
I had used an end rule and several variations but was pretty stuck until I realized sed was the easier way to go.

Thanks for your help.



 
We can look at this another way if we use nawk. Lets say you have 2 shell variables val1 and val2 which represent line numbers, and you want to print/manipulate all lines between those numbers. Using nawk, you can use the -v switch to assign a value to a nawk variable which it can access inside the nawk program.

Assume val1=2 and val2=112. To display all lines between 2 and 112, we can say:

nawk -v sr=$val1 -v er=$val2 'NR >=sr && NR<=er {print}' input_file

This, in my opinion, is a neater way. I'm only starting to learn the value & power offered by thinking about how to use awk/nawk pattern matching ability, but it's certainly worth studying for a while :)

HTH.

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top