0

I want to add \t before each element in the resulting jq array and this is for formatting purposes as the result will get added to a file. here is the json file:

{
    "array": ["element1", "element2"]
}

When you read this array with jq content=$(jq '.array' file) and echo "$content" we get:

[
   "element1",
   "element2"
]

What I want to have this:

[
  \t\t"element1",
  \t\t"element2"
\t\t]

What is the best way to do this?

5
  • 1
    You want to indent everything but the first line by two tabs, or do you want to insert the literal string \t\t? Are you adding this array to another JSON file? If so, it would be better to do that withjq directly rather than via some sed processing. Commented May 10, 2024 at 11:11
  • No i'm not adding this in another json file, but in a another text configuration file. And isn't inserting \t\t equal to indenting by two tabs? Commented May 10, 2024 at 12:29
  • You never once mention the word "tab" in th e question, which is why I was wondering whether you meant the two characters \ and t, or a literal tab character. Also, you seem to have accepted an answer, even though this generates output that is not like that in the question (the tabs are at the start of the line rather than at the start of each element). Since you mentioned "before each element" rather than "at the start of the line", I thought this was an important distinction to make. Commented May 10, 2024 at 13:29
  • Whether it's at the end or beginning it wouldn't matter, because whether I have ' \t\t' or '\t\t ' is similar at the end. Commented May 10, 2024 at 13:41
  • @kyles the point is it's not clear from your question if you want the 4 characters \, t, \, t or 2 literal tab characters inserted. Inserting \t\t is not the same as inserting 2 tab chars, \t is an escape sequence that some tools will use in output statements to generate tab chars but that doesn't mean \t is a tab char, and at no point do you say in your question that you want tab characters but instead show literal \ts in your expected output. If you're going to show that then at least say "those \ts are literal tab chars" or similar in your question. Commented May 10, 2024 at 15:31

1 Answer 1

1

Here is the code that adds tabs before each line:

jq '.array' file | sed '1!s/^/\t\t/'

It adds \t\t before each line except the first one (if that's exactly what you wanted)

4
  • 1
    Note that they might have wanted the literal string \t\t, not tabs. Also, note that the string is not added at the start of each line, but before the first non-space charcters. Commented May 10, 2024 at 11:18
  • Thanks for mentioning @Kusalananda, if a code other than the code I provided was asked I'll edit the answer to fit the question. Commented May 10, 2024 at 11:19
  • Additionally, the given input file isn't even parseable by jq (it's not JSON), so we definitely need clarification from the user before giving any sort of authoritative answer. Commented May 10, 2024 at 11:24
  • Assuming that the lack of quotations around the word array is a typo‌ (I may definitely be wrong about that) the answer provides what it claims: It adds \t\t before each line except the first one (if that's exactly what you wanted) Commented May 10, 2024 at 11:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.