我在查找字符串(topic-link)时遇到了一些麻烦,然后使用awk附加了一个递增的数字。
我有以下html文件,这一行都是一行:
<a class="topic-link" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link" href="test.com/topic/weight-gain">Weight Gain</a> </p>
使用awk我试图获得:
<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link2" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link3" href="test.com/topic/weight-gain">Weight Gain</a> </p>
我正在运行以下内容:
awk '{gsub("topic-link","topic-link"++i)}1' input file > output file
问题是结果如下:
<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link1" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link1" href="test.com/topic/weight-gain">Weight Gain</a> </p>
我提出的解决方案只有在“主题链接”的实例分开的情况下才能正常工作,因此我被卡住了。
请告诉我,我遗漏了一些非常明显的内容,或者您对替代方法有任何建议。
谢谢!
Rylan
答案 0 :(得分:1)
你的gsub()只被调用一次所以我只增加一次。你需要一个循环:
$q = "
select
(
select
coalesce(
(
select count(distinct b.username)
from recent b
where
b.istopscore = 1 AND
(
(
b.score > a.score AND
b.time <= a.time
) OR
(
b.score = a.score AND
b.username != a.username AND
b.time < a.time
)
)
), 0) + 1 Rank
from scores a
where a.nickname = ?) as Rank,
t.time,
t.username,
t.score
from
scores t
WHERE t.nickname = ?
";
$r_time = 0;
if( $stmt = $mysqli->prepare( $q ) )
{
$stmt->bind_param( 'ss', $nick, $nick );
$stmt->execute();
$stmt->store_result();
$stmt->bind_result( $r_rank, $r_time, $r_username, $r_score );
$stmt->fetch();
if( intval($r_rank) > 99999 )
$r_rank = 99999;
$stmt->close();
}
// Previous Rank
$r_prevrank = -1;
if( $r_rank > -1 )
{
$q = "
select
coalesce(
(
select count(distinct b.username)
from recent b
where
b.istopscore = 1 AND
(
(
b.score > a.score AND
b.time <= a.time
) OR
(
b.score = a.score AND
b.username != a.username AND
b.time < a.time
)
)
), 0) + 1 Rank
from recent a
where a.username = ? and a.time < ? and a.score < ?
order by score desc limit 1";
if( $stmt = $mysqli->prepare( $q ) )
{
$time_minus_one = ( $r_time - 1 );
$stmt->bind_param( 'sii', $r_username, $time_minus_one, $r_score );
$stmt->execute();
$stmt->store_result();
$stmt->bind_result( $r_prevrank );
$stmt->fetch();
if( intval($r_prevrank) > 99999 )
$r_prevrank = 99999;
$stmt->close();
}
$drop = ($current_rank - $r_rank);
$drop = ($drop > 0 ? $drop : 0 );
$increase = $r_prevrank - $r_rank;
$increase = ($increase > 0 ? $increase : 0 );
//$change = $increase - $drop;
$change = ($drop > 0 ? -$drop : $increase);
}
return $change;