简体   繁体   English

在 bash 脚本中使用 awk

[英]Using awk in a bash-script

I've been struggling to run an awk result as the arguments of a new awk command within a bash-script;我一直在努力运行 awk 结果作为 bash 脚本中新 awk 命令的 arguments ; something like this像这样的东西

    echo $(
        ag '^@' ~/dotfiles/shared/journal | 
        sed  's/\(^.*\.txt:\)\(.*\)/\2 \| \1/g' | 
        fzf | 
        awk -F\| 'BEGIN{OFS="";} { print "\"/", substr($1, 3, length($1)-3), "/,/^~+$/","\" ", substr($2, 1, length($2)-1) }' 
    ) | xargs -0 -I "{}" awk {}

To help eradicate (what I think is) inconsequential code to this problem,为了帮助消除(我认为是)对这个问题无关紧要的代码,

    echo $(
        ag '^@' ~/dotfiles/shared/journal | 
        sed  's/\(^.*\.txt:\)\(.*\)/\2 \| \1/g' | 
        fzf --height=40% --layout=reverse --info=inline --border --margin=1 | 
        awk -F\| 'BEGIN{OFS="";} { print "\"/", substr($1, 3, length($1)-3), "/,/^~+$/","\" ", substr($2, 1, length($2)-1) }' 
)

gives me给我

"/@ Search Pattern/,/^~+$/" /home/sam/dotfiles/shared/journal/20210125.txt

and if prepend awk to this result, I get the answer I need.如果在这个结果前面加上 awk,我会得到我需要的答案。 But I'm not able to make it work with xargs;但我无法使其与 xargs 一起使用; it gives no output... Also, if I remove the xargs pipe, and substitue echo with awk , I get它没有给出 output ... 另外,如果我删除 xargs pipe,并用awk代替echo ,我得到

awk: cmd. line:1: "/@
awk: cmd. line:1: ^ unterminated string

If I change the relevant quotation marks like so如果我像这样更改相关引号

awk -F\| 'BEGIN{OFS="";} { print "\047/", substr($1, 3, length($1)-3), "/,/^~+$/","\047 ", substr($2, 1, length($2)-1) }'

I end up with this error我最终遇到了这个错误

awk: cmd. line:1: '/@
awk: cmd. line:1: ^ invalid char ''' in expression

Is it even possible to create an awk command using the result of another awk-command?甚至可以使用另一个 awk 命令的结果创建 awk 命令吗?

EDIT: with more explanations Here's an over-all picture of what I'm trying to do... I have a bunch of text files in my ~/dotfiles/shared/journal folder that have the following format编辑:有更多解释这是我正在尝试做的全面图片...我的~/dotfiles/shared/journal文件夹中有一堆文本文件,格式如下

@ Project Title 

# Heading 1
some random text here, you know, typical markdown stuff

# Another Heading
Something **bold** here, etc., basically a typical markdown file

~~~
@ Another Project Title
# Second Project's Heading 
Some more markdown 

~~~

So the ag script goes through all the files, and lists out all the project titles (so conveniently prefixed with the @ )所以 ag 脚本会遍历所有文件,并列出所有项目标题(以@为前缀很方便)

the sed line makes it all pretty to be piped into fzf, An average entry would look like this sed 线使其非常适合通过管道传输到 fzf,平均条目看起来像这样

2:@ Project Title | /home/sam/dotfiles/shared/journal/20210125.txt:

the awk line basically would take the output and reformat it so that it could be made into an argument, (looking like this) awk 线基本上会采用 output 并重新格式化它,以便它可以成为一个参数,(看起来像这样)

'/@ Project Title/,/^~+$/' /home/sam/dotfiles/shared/journal/20210125.txt

So now, if I just take this result and prefix it with awk on my commandline like so所以现在,如果我只取这个结果并在我的命令行上用 awk 作为前缀,就像这样

awk '/@ Project Title/,/^~+$/' /home/sam/dotfiles/shared/journal/20210125.txt

I would get我会得到

@ Project Title
# Heading 1
some random text here, you know, typical markdown stuff

# Another Heading
Something **bold** here, etc., basically a typical markdown file
~~~

But if I replace echo with awk, I get the errors mentioned above.但是如果我用 awk 替换echo ,我会得到上面提到的错误。

End of EDIT编辑结束

** Side Note ** I would eventually want to pipe the markdown into weasyprint, but one thing at a time... **旁注**我最终想将pipe markdown放入weasyprint,但一次一件事......

Your quoting is not really going to make it through xargs the way you would like.您的报价并不会真正按照您想要的方式通过xargs进行。 It's not impossible to pull off, but I would instead do something like这不是不可能的,但我会做类似的事情

ag '^@' ~/dotfiles/shared/journal | 
sed  's/\(^.*\.ejrnl:\)\(.*\)/\2 \| \1/g' | 
fzf |
awk -F\| 'BEGIN{OFS="";} { print "/", substr($1, 3, length($1)-3), "/,/^~+$/","\" ", substr($2, 1, length($2)-1) }' 
| sh

However, this could still go wrong if you have several identical titles in your file.但是,如果您的文件中有多个相同的标题,这仍然可能是 go 错误。 A more robust as well as simpler approach would extract the start and end line numbers while you are extracting the titles, and then just simply pass those through when you want to extract the whole journal entry.一种更强大且更简单的方法是在您提取标题时提取开始和结束行号,然后在您想要提取整个日记条目时简单地传递它们。 Something like (untested)类似的东西(未经测试)

awk '/^@/ { if(prev) print pf ":" prev "," FNR-1 ":" ptitle;
    prev=FNR; pf=FILENAME; ptitle=$0; }
END { if (prev) print pf ":" prev "," FNR ":" ptitle }' ~/dotfiles/shared/journal/**/*.md |
fzf --height=40% --layout=reverse --info=inline --border --margin=1 | 
while IFS=: read -r file lines _; do
    sed -n "$lines"p "$file"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM