- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 awk
脚本进行一些处理并将其输出发送到文件。我如何在我的 awk
程序的 BEGIN block 中写出一条类似横幅的消息首先到那个文件,比如 bash heredoc
。
我知道我可以使用多个 print
命令,但是有什么方法可以让一个 print
命令,但保留带有换行符等的多行文本。
所以输出应该是这样的:
#########################################
# generated by some author #
# ENVIRON["VAR"]
#########################################
漂亮格式的另一个问题是 ENVIRON["VAR"]
应该是在字符串中间展开。
最佳答案
简单的方法是使用 heredoc 并将其保存在 awk 变量中:
VAR="whatever"
awk -v var="\
#########################################
# generated by some author #
# $VAR
#########################################" '
BEGIN{ print var }
'
#########################################
# generated by some author #
# whatever
#########################################
或者,这可能比您想要的更多,但下面是我用来提供比 awk 中的此处文档更好的东西的命令。我发现在将模板文本添加到多个文件时,它绝对是无价的。
这是一个 shell 脚本,它接受一个带有稍微扩展语法的 awk 脚本(以方便此处的文档)作为输入,调用 gawk 将扩展语法转换为普通的 awk 打印语句,然后再次调用 gawk 来执行生成的脚本。
我称它为“epawk”,表示“扩展打印”awk,下面是该工具以及几个如何使用它的示例。当您调用它而不是直接调用 awk 时,您可以编写脚本,其中包含用于打印的预格式化文本 block ,就像您想要使用 here-doc 一样(每个 #
之前的空格是一个制表符字符):
$ export VAR="whatever"
$ epawk 'BEGIN {
print <<-!
#########################################
# generated by some author #
# "ENVIRON["VAR"]"
#########################################
!
}'
#########################################
# generated by some author #
# whatever
#########################################
它的工作原理是从您的 awk 脚本创建一个 awk 脚本,然后执行它。如果你只是想查看正在生成的脚本,epawk
将打印生成的脚本而不是执行它,如果你给它 -X
参数,例如:
$ epawk -X 'BEGIN {
print <<-!
#########################################
# generated by some author #
# "ENVIRON["VAR"]"
#########################################
!
}'
BEGIN {
print "#########################################"
print "# generated by some author #"
print "# "ENVIRON["VAR"]""
print "#########################################"
}
脚本:
$ cat epawk
#!/usr/bin/env bash
# The above must be the first line of this script as bash or zsh is
# required for the shell array reference syntax used in this script.
##########################################################
# Extended Print AWK
#
# Allows printing of pre-formatted blocks of multi-line text in awk scripts.
#
# Before invoking the tool, do the following IN ORDER:
#
# 1) Start each block of pre-formatted text in your script with
# print << TERMINATOR
# on it's own line and end it with
# TERMINATOR
# on it's own line. TERMINATOR can be any sequence of non-blank characters
# you like. Spaces are allowed around the symbols but are not required.
# If << is followed by -, e.g.:
# print <<- TERMINATOR
# then all leading tabs are removed from the block of pre-formatted
# text (just like shell here documents), if it's followed by + instead, e.g.:
# print <<+ TERMINATOR
# then however many leading tabs are common across all non-blank lines
# in the current pre-formatted block are removed.
# If << is followed by =, e.g.
# print <<= TERMINATOR
# then whatever leading white space (tabs or blanks) occurs before the
# "print" command will be removed from all non-blank lines in
# the current pre-formatted block.
# By default no leading spaces are removed. Anything you place after
# the TERMINATOR will be reproduced as-is after every line in the
# post-processed script, so this for example:
# print << HERE |"cat>&2"
# foo
# HERE
# would cause "foo" to be printed to stderr.
#
# 2) Within each block of pre-formatted text only:
# a) Put a backslash character before every backslash (\ -> \\).
# b) Put a backslash character before every double quote (" -> \").
# c) Enclose awk variables in double quotes without leading
# backslashes (awkVar -> "awkVar").
# d) Enclose awk record and field references ($0, $1, $2, etc.)
# in double quotes without leading backslashes ($1 -> "$1").
#
# 3) If the script is specified on the command line instead of via
# "-f script" then replace all single quote characters (') in or out
# of the pre-formatted blocks with their ANSI octal escape sequence (\047)
# or the sequence '\'' (tick backslash tick tick). This is normal and is
# required because command-line awk scripts cannot contain single quote
# characters as those delimit the script. Do not use hex \x27, see
# http://awk.freeshell.org/PrintASingleQuote.
#
# Then just use it like you would gawk with the small caveat that only
# "-W <option>", not "--<option>", is supported for long options so you
# can use "-W re-interval" but not "--re-interval" for example.
#
# To just see the post-processed script and not execute it, call this
# script with the "-X" option.
#
# See the bottom of this file for usage examples.
##########################################################
expand_prints() {
gawk '
!inBlock {
if ( match($0,/^[[:blank:]]*print[[:blank:]]*<</) ) {
# save any blanks before the print in case
# skipType "=" is used.
leadBlanks = $0
sub(/[^[:blank:]].*$/,"",leadBlanks)
$0 = substr($0,RSTART+RLENGTH)
if ( sub(/^[-]/,"") ) { skipType = "-" }
else if ( sub(/^[+]/,"") ) { skipType = "+" }
else if ( sub(/^[=]/,"") ) { skipType = "=" }
else { skipType = "" }
gsub(/(^[[:blank:]]+|[[:blank:]]+$)/,"")
if (/[[:blank:]]/) {
terminator = $0
sub(/[[:blank:]].*/,"",terminator)
postprint = $0
sub(/[^[:blank:]]+[[:blank:]]+/,"",postprint)
}
else {
terminator = $0
postprint = ""
}
startBlock()
next
}
}
inBlock {
stripped=$0
gsub(/(^[[:blank:]]+|[[:blank:]]+$)/,"",stripped)
if ( stripped"" == terminator"" ) {
endBlock()
}
else {
updBlock()
}
next
}
{ print }
function startBlock() { inBlock=1; numLines=0 }
function updBlock() { block[++numLines] = $0 }
function endBlock( i,numSkip,indent) {
if (skipType == "") {
# do not skip any leading tabs
indent = ""
}
else if (skipType == "-") {
# skip all leading tabs
indent = "[\t]+"
}
else if (skipType == "+") {
# skip however many leading tabs are common across
# all non-blank lines in the current pre-formatted block
for (i=1;i<=numLines;i++) {
if (block[i] ~ /[^[:blank:]]/) {
match(block[i],/^[\t]+/)
if ( (numSkip == "") || (numSkip > RLENGTH) ) {
numSkip = RLENGTH
}
}
}
for (i=1;i<=numSkip;i++) {
indent = indent "\t"
}
}
else if (skipType == "=") {
# skip whatever pattern of blanks existed
# before the "print" statement
indent = leadBlanks
}
for (i=1;i<=numLines;i++) {
sub(indent,"",block[i])
print "print \"" block[i] "\"\t" postprint
}
inBlock=0
}
' "$@"
}
unset awkArgs
unset scriptFiles
expandOnly=0
while getopts "v:F:W:f:X" arg
do
case $arg in
f ) scriptFiles+=( "$OPTARG" ) ;;
[vFW] ) awkArgs+=( "-$arg" "$OPTARG" ) ;;
X ) expandOnly=1 ;;
* ) exit 1 ;;
esac
done
shift $(( OPTIND - 1 ))
if [ -z "${scriptFiles[*]}" -a "$#" -gt "0" ]
then
# The script cannot contain literal 's because in cases like this:
# 'BEGIN{ ...abc'def... }'
# the args parsed here (and later again by gawk) would be:
# $1 = BEGIN{ ...abc
# $2 = def... }
# Replace 's with \047 or '\'' if you need them:
# 'BEGIN{ ...abc\047def... }'
# 'BEGIN{ ...abc'\''def... }'
scriptText="$1"
shift
fi
# Remaining symbols in "$@" must be data file names and/or variable
# assignments that do not use the "-v name=value" syntax.
if [ -n "${scriptFiles[*]}" ]
then
if (( expandOnly == 1 ))
then
expand_prints "${scriptFiles[@]}"
else
gawk "${awkArgs[@]}" "$(expand_prints "${scriptFiles[@]}")" "$@"
fi
elif [ -n "$scriptText" ]
then
if (( expandOnly == 1 ))
then
printf '%s\n' "$scriptText" | expand_prints
else
gawk "${awkArgs[@]}" "$(printf '%s\n' "$scriptText" | expand_prints)" "$@"
fi
else
printf '%s: ERROR: no awk script specified.\n' "$toolName" >&2
exit 1
fi
使用示例:
$ cat data.txt
abc def"ghi
.
#######
$ cat script.awk
{
awkVar="bar"
print "----------------"
print << HERE
backslash: \\
quoted text: \"text\"
single quote as ANSI sequence: \047
literal single quote (ONLY works when script is in a file): '
awk variable: "awkVar"
awk field: "$2"
HERE
print "----------------"
print <<-!
backslash: \\
quoted text: \"text\"
single quote as ANSI sequence: \047
literal single quote (ONLY works when script is in a file): '
awk variable: "awkVar"
awk field: "$2"
!
print "----------------"
print <<+ whatever
backslash: \\
quoted text: \"text\"
single quote as ANSI sequence: \047
literal single quote (ONLY works when script is in a file): '
awk variable: "awkVar"
awk field: "$2"
whatever
print "----------------"
}
.
$ epawk -f script.awk data.txt
----------------
backslash: \
quoted text: "text"
single quote as ANSI sequence: '
literal single quote (ONLY works when script is in a file): '
awk variable: bar
awk field: def"ghi
----------------
backslash: \
quoted text: "text"
single quote as ANSI sequence: '
literal single quote (ONLY works when script is in a file): '
awk variable: bar
awk field: def"ghi
----------------
backslash: \
quoted text: "text"
single quote as ANSI sequence: '
literal single quote (ONLY works when script is in a file): '
awk variable: bar
awk field: def"ghi
----------------
.
$ epawk -F\" '{
print <<!
ANSI-tick-surrounded quote-separated field 2 (will work): \047"$2"\047
!
}' data.txt
ANSI-tick-surrounded quote-separated field 2 (will work): 'ghi'
.
epawk -F\" '{
print <<!
Shell-escaped-tick-surrounded quote-separated field 2 (will work): '\''"$2"'\''
"
}' data.txt
Shell-escaped-tick-surrounded quote-separated field 2 (will work): 'ghi'
.
$ epawk -F\" '{
print <<!
Literal-tick-surrounded quote-separated field 2 (will not work): '"$2"'
!
}' data.txt
Literal-tick-surrounded quote-separated field 2 (will not work):
.
$ epawk -X 'BEGIN{
print <<!
foo
bar
!
}'
BEGIN{
print " foo"
print " bar"
}
.
$ cat file
a
b
c
.
$ epawk '{
print <<+! |"cat>o2"
numLines="NR"
numFields="NF", $0="$0", $1="$1"
!
}' file
.
$ cat o2
numLines=1
numFields=1, $0=a, $1=a
numLines=2
numFields=1, $0=b, $1=b
numLines=3
numFields=1, $0=c, $1=c
.
$ epawk 'BEGIN{
cmd = "sort"
print <<+! |& cmd
d
b
a
c
!
close(cmd, "to")
while ( (cmd |& getline line) > 0 ) {
print "got:", line
}
close(cmd)
}' file
got: a
got: b
got: c
got: d
关于file - 从 awk 脚本打印文本 block 到文件 [banner like],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596514/
以下标记从验证器收到错误: Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla
我在 ViewWillAppear 和 ViewWillDisappear 上分别创建和删除了 ADBannerView,尽管在切换到另一个未创建 ADBannerView 的 View 后,我仍然看
我想在访问者访问 page.html 时显示 bannerA.js,并在访问者访问 page_en.html 时显示 bannerB.js 等等,5 个横幅/5 个页面。 我看到我可以使用这样的代码:
有谁知道如何在 cocos 2d v2 中使用 admob,所有文档都基于 View 根 Controller ,而 cocos2d 2 只是以另一种方式进行。 我找到的唯一文档是:Working-w
我有一个使用 ASP MVC 4 的新大项目,我需要在未来构建许多微型 Web 应用程序,其中包含: 一个Banner(比如黑色的Google banner,用于身份验证、通知、搜索……等) 必须访问
我最近写了一个小脚本来获取本地网络中的op ssh服务器列表,因为我不知道计算机的ip地址,而无需将它们连接到屏幕并进行查找(这将消除对ssh的需要)。因为我有多个ssh服务器,所以我想知道哪个IP地
为了搬openx,我经历了整个过程。我的横幅通常会显示,我可以完全访问管理。唯一的问题是,当尝试上传横幅时,它不会被保存。当返回横幅属性选项卡时,该文件丢失了。你能帮忙吗?谢谢。 最佳答案 将文件迁移
任何人都可以帮助我获得一个简单的库,该库具有滑动图像横幅的功能, slider 上带有点以显示当前存在的图像。我试过 ViewPager。由于我是 Android 的新手,所以我不知道该用什么。我尝试
我有 iAd 的问题。我遵循了许多质量上乘的教程,几天前我能够在测试应用程序中正确插入 iAd。 现在,即使我尝试创建一个仅使用 iAd 作为测试的新应用程序,该方法也不起作用!我无法理解以下文字出现
在 Banner Landmark 的 W3C ARIA 示例页面中HTML 技术 选项卡中有这段文字:(强调我的) The HTML5 header element defines a banner
示例:Ads by: Google 这是横幅 css,我想在这个横幅中添加左上角(广告:mysite.com) .mybannerads { display: block; posi
我正在开发一个需要展示 AdMob 的 横幅广告 的 Flutter 应用程序。我注意到横幅重叠我的 ListView 。我试图搜索解决方案,但没有发现任何有用的东西。 我发现的一个解决方案是在底部提
您好,我想修改默认的 joomla 横幅模块,这样不仅可以显示横幅图像,还可以显示已在后端输入的描述文本。你能帮助我吗?谢谢 最佳答案 如果您使用默认 mod_banner的 Joomla ,您需要在
我在 App Store 中有一个名为“Rollerbank”的荷兰语应用程序。在荷兰,允许使用 iAd。所以在我的应用程序中我有“self.canDisplayBannerAds = YES;”在我
我正在用 php/mysql 编写横幅广告引擎。我不想使用 OpenX 或交 key 解决方案,因为会有一堆自定义功能,我宁愿不依赖于必须灵活适应的现有系统。 以下是我目前对印象架构的思考和方法: 通
我需要在一个列表项上添加“即将推出”横幅。横幅应该相对于 li 绝对定位,以便它看起来环绕 li。 我有问题: 让横幅相对于 li 绝对定位 隐藏列表项停止处的横幅(这样在视觉上它似乎只环绕内容)。就
有人对如何在横幅底部放置小图片有任何建议吗?请参阅此附件(以红色圈出)以了解我在说什么。我可以使用 CSS3 做到这一点,还是我最好只将图像放在那里?如果该图像可以根据每个页面的标题文本(例如“我们的
我将使用全屏横幅,只是like this one或 this other one为此,我在 Wordpress 站点中使用了一个插件。 现在我已将它们各自的宽度设置为 100%(以避免 x 轴滚动并使
我希望有人能提供帮助。 我有一个横幅旋转器,它在一个页面上工作正常,但在另一个页面上却不行。我刚刚将代码从一个页面复制并粘贴到另一个页面,并更改了路径,以便它们指向正确的位置。我已经检查过一切都是一样
我想将一个元素转换成一个横幅,它填充一个 div 而不会变形。 正如您在我的图片上看到的那样,我希望图片 1 像图片 2 一样对原始图片进行缩放,就像我对图片 3 所做的那样。 我用 css 尝试了很
我是一名优秀的程序员,十分优秀!