grep nth line

博客给出了两个信息技术领域的命令示例。一是使用awk命令提取文件file的第45行内容;二是用sed命令打印文件someFile中从第1234行到第5555行的内容。

awk 'NR==45' file

sed -n '1234,5555p' someFile

#!/bin/sh #![allow(missing_abi)] /* # rust shebang by Mahmoud Al-Qudsi, Copyright NeoSmart Technologies 2020-2024 # See <https://neosmart.net/blog/self-compiling-rust-code/> for info & updates. # # This code is freely released to the public domain. In case a public domain # license is insufficient for your legal department, this code is also licensed # under the MIT license. # Get an output path that is derived from the complete path to this self script. # - `realpath` makes sure if you have two separate `script.rs` files in two # different directories, they get mapped to different binaries. # - `which` makes that work even if you store this script in $PATH and execute # it by its filename alone. # - `cut` is used to print only the hash and not the filename, which `md5sum` # always includes in its output. OUT=/tmp/$(printf "%s" $(realpath $(which "$0")) | md5sum | cut -d' ' -f1) # Calculate hash of the current contents of the script, so we can avoid # recompiling if it hasn't changed. MD5=$(md5sum "$0" | cut -d' ' -f1) # Check if we have a previously compiled output for this exact source code. if !(test -x "${OUT}" && test -f "${OUT}.md5" && \ test "${MD5}" = "$(cat "${OUT}.md5")"); then # The script has been modified or is otherwise not cached. # Check if the script already contains an `fn main()` entry point. if grep -Eq '^\s*(\[([^][]*)])*\s*fn\s+main\b' "$0"; then # Compile the input script as-is to the previously determined location. rustc "$0" -o ${OUT} # Save rustc's exit code so we can compare against it later. RUSTC_STATUS=$? else # The script does not contain an `fn main()` entry point, so add one. # We don't use `printf 'fn main() { %s }' because the shebang must # come at the beginning of the line, and we don't use `tail` to skip # it because that would result in incorrect line numbers in any errors # reported by rustc, instead we just comment out the shebang but leave # it on the same line as `fn main() {`. printf "fn main() {//%s\n}" "$(cat $0)" | rustc - -o ${OUT} # Save rustc's exit code so we can compare against it later. RUSTC_STATUS=$? fi # Check if we compiled the script OK, or exit bubbling up the return code. if test "${RUSTC_STATUS}" -ne 0; then exit ${RUSTC_STATUS} fi # Save the MD5 of the current version of the script so we can compare # against it next time. printf "%s" ${MD5} > ${OUT}.md5 fi # Execute the compiled output. This also ends execution of the shell script, # as it actually replaces its process with ours; see exec(3) for more on this. exec ${OUT} "$@" # At this point, it's OK to write raw rust code as the shell interpreter # never gets this far. But we're actually still in the rust comment we opened # on line 2, so close that: */ fn main() { let name = std::env::args().skip(1).next().unwrap_or("world".into()); println!("Hello, {}!", name); } // vim: ft=rust : 解释以下这段代码
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值