Skip to content
Advertisement

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; something like this

JavaScript

To help eradicate (what I think is) inconsequential code to this problem,

JavaScript

gives me

JavaScript

and if prepend awk to this result, I get the answer I need. But I’m not able to make it work with xargs; it gives no output… Also, if I remove the xargs pipe, and substitue echo with awk, I get

JavaScript

If I change the relevant quotation marks like so

JavaScript

I end up with this error

JavaScript

Is it even possible to create an awk command using the result of another awk-command?

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

JavaScript

So the ag script goes through all the files, and lists out all the project titles (so conveniently prefixed with the @)

the sed line makes it all pretty to be piped into fzf, An average entry would look like this

JavaScript

the awk line basically would take the output and reformat it so that it could be made into an argument, (looking like this)

JavaScript

So now, if I just take this result and prefix it with awk on my commandline like so

JavaScript

I would get

JavaScript

But if I replace echo with awk, I get the errors mentioned above.

End of EDIT

** Side Note ** I would eventually want to pipe the markdown into weasyprint, but one thing at a time…

Advertisement

Answer

Your quoting is not really going to make it through xargs the way you would like. It’s not impossible to pull off, but I would instead do something like

JavaScript

However, this could still go wrong if you have several identical titles in your file. 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)

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement