问题描述
#!的/ usr / local / bin目录/ GAWK -f`{
awkvar2 =/ ID = 22 /;
awkvar3 =/挂断/;
如果($ 0〜awkvar2){
触发= 1;
}
如果(触发){
打印;
如果($ 0〜awkvar3){
触发= 0;
打印\\ n ---------------------------------------------- - \\ n
}
}
}
awk脚本不是为我工作
我试图寻找从一行到另一个即 ID = 22
到结束
(我不使用的原因 /<串> /,/<串> /
是因为我想要搜索的每个块之后,大线)
,我想这只能使用变量。结果
我可以直接使用模式如果($ 0〜/挂断/){
但我不想这样做,我想使用的搜索模式里面的变量
(原因我将在动态的变量彻底壳获取的值)
请告诉我如何使用变量搜索模式里面AWK
感谢...
{
awkvar2 =ID = 22;
awkvar3 =结束;
如果($ 0〜awkvar2){
触发= 1;
}
如果(触发){
打印;
如果($ 0〜awkvar3){
触发= 0;
打印\\ n ---------------------------------------------- - \\ n
}
}
}
修改
每个请求修改打印ID = 22
前行 {
awkvar2 =ID = 22;
awkvar3 =结束;
如果($ 0〜awkvar2){
打印preV;
触发= 1;
}
如果(触发){
打印;
如果($ 0〜awkvar3){
触发= 0;
打印\\ n ---------------------------------------------- - \\ n
}
}
{preV = $ 0;}
}
或者,更的 awkish 的
BEGIN {awkvar2 =ID = 22; awkvar3 =结束}($ 0〜awkvar2),($ 0〜awkvar3){如果($ 0〜awkvar2){打印preV;}
打印;
如果($ 0〜awkvar3){
打印\\ n --------------- \\ n
}
}
{preV = $ 0;}
#!/usr/local/bin/gawk -f `
{
awkvar2="/id=22/";
awkvar3="/end/";
if ($0 ~ awkvar2) {
triggered=1;
}
if (triggered) {
print;
if ($0 ~ awkvar3) {
triggered=0;
print "\n-----------------------------------------------\n"
}
}
}
this awk script is not working for mei am trying to search from one line to another i.e id=22
till end
(the reason i am not using /<string>/,/<string>/
is because i want a big line after each block of search)and i want this using variables only.
i could directly use the patterns if ($0 ~ /end/) {
but i dont want to do that, i want to use the variables inside the search pattern(reason is i will be getting the values in the variables dynamically thorough the shell)
please advise me how to use variables inside the search pattern for awk
thanks...
{
awkvar2="id=22";
awkvar3="end";
if ($0 ~ awkvar2) {
triggered=1;
}
if (triggered) {
print;
if ($0 ~ awkvar3) {
triggered=0;
print "\n-----------------------------------------------\n"
}
}
}
Edit
Modified per request to print the line before "id=22"
{
awkvar2="id=22";
awkvar3="end";
if ($0 ~ awkvar2) {
print prev;
triggered=1;
}
if (triggered) {
print;
if ($0 ~ awkvar3) {
triggered=0;
print "\n-----------------------------------------------\n"
}
}
{prev=$0;}
}
Or, more awkish
BEGIN {awkvar2="id=22";awkvar3="end"}
($0 ~ awkvar2),($0 ~ awkvar3) { if ($0 ~ awkvar2) {print prev;}
print;
if ($0 ~ awkvar3) {
print "\n---------------\n"
}
}
{prev=$0;}
这篇关于使用搜索模式变量awk脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!