I am looking for a program, which concatenates strings with a char, prepends one char and appends another. I think I have used the wrong keywords for my search, but I was not able to find the perfect unix tool for that issue.
Suppose I have a file (note the starting empty lines):
file in.txt
:
{
"some": "json",
"with_different": "intendation",
"which": [],
"has": 2
}
{
"json":"objects"
}
and generate out.txt
[
{
"some": "json",
"with_different": "intendation",
"which": [],
"has": 2
}
,
{
"json":"objects"
}
]
Basically, I want a JSON-array from that, meaning:
- get rid of first empty lines (
uniq | tail --lines=+2
), - replace empty lines with comma (
sed -e 's/^$/,g/'
) and - prepend/append it with
[
and]
(awk 'BEGIN {print "["} {print $1} END {print "]"}
).
uniq <in.txt | tail --lines=+2 | sed -e 's/^$/,/g' | awk 'BEGIN {print "["} {print $1} END {print "]"}'
is giving me what I want, but I sure think, that this is not elegant.
I have found paste
, xargs
, join
, but they do not help me. Also I know about the OFS
variable in awk
, which may replace the sed
part, but I don't know how to convince awk
to treat all 'non-empty' lines as $1 (probably using IFS
, but IFS='^$' is surely not working.) And then we still have the other boilerplate around it.
I am hoping that someone can point me to magic
-program like magic -d"," -s"[" -e"]" <in
, provided I have cleaned the empty lines above, or the objects are one-liners
file in
:
{"some":"json", "which":[], "has": 2}
{ "json":"objects"}
to file out
:
[
{"some":"json", "which":[], "has": 2}
,
{ "json":"objects"}
]
Other example would be echo "a b c" | magic -d',' -s'[' -e']'
returns [a,b,c]
.
Or, to not only give JSON
examples:
echo "my new component" | magic -d'-' -s'<' -e'>'
returns <my-new-component>
.
Notes:
jq -s .
would work for thisjson
-problem (cf. How to combine the sequence of objects in jq into one object?) but if the start/end/delim chars are something else it wouldn't.- I am fine with line breaks being removed.
- I would really like to have a shorter one-liner than my own attempt