有时我想要一个bash脚本,它主要是一个帮助文件。可能有更好的方法来做事,但有时我想要一个名为" awk_help"的文件。我跑了,它把我的awk笔记转储到终端。
我怎样才能轻松完成这项工作?
答案 0 :(得分:1)
另一个想法,使用#!/bin/cat
- 这将完全回答你问题的标题,因为也会显示shebang线。
答案 1 :(得分:0)
事实证明,它可以作为一个单一的班轮来完成,感谢@CharlesDuffy的建议!
只需将以下内容放在文件顶部,即可完成
cat "$BASH_SOURCE" | grep -v EZREMOVEHEADER
因此,对于我的awk_help
示例,它是:
cat "$BASH_SOURCE" | grep -v EZREMOVEHEADER
# Basic form of all awk commands
awk search pattern { program actions }
# advanced awk
awk 'BEGIN {init} search1 {actions} search2 {actions} END { final actions }' file
# awk boolean example for matching "(me OR you) OR (john AND ! doe)"
awk '( /me|you/ ) || (/john/ && ! /doe/ )' /path/to/file
# awk - print # of lines in file
awk 'END {print NR,"coins"}' coins.txt
# Sum up gold ounces in column 2, and find out value at $425/ounce
awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt
# Print the last column of each line in a file, using a comma (instead of space) as a field separator:
awk -F ',' '{print $NF}' filename
# Sum the values in the first column and pretty-print the values and then the total:
awk '{s+=$1; print $1} END {print "--------"; print s}' filename
# functions available
length($0) > 72, toupper,tolower
# count the # of times the word PASSED shows up in the file /tmp/out
cat /tmp/out | awk 'BEGIN {X=0} /PASSED/{X+=1; print $1 X}'
# awk regex operators
https://www.gnu.org/software/gawk/manual/html_node/Regexp-Operators.html
答案 2 :(得分:0)
我发现了另一种可在Mac / Linux上运行且完全符合人们期望的解决方案。
只需将以下内容用作您的“ shebang”行,它将输出从第2行开始的所有内容:
#!/usr/bin/tail -n+2
hi there
how are you
运行此命令可以为您带来期望的结果:
$ ./test.sh
hi there
how are you