4

I have a file, actually json file. How I can add a comma to the end of every but the last line ? For example:

{"a":1, "b":1, "c":2}
{"a":3, "b":3, "c":1}
{"a":1, "b":2, "c":3}

What I want:

{"a":1, "b":1, "c":2},
{"a":3, "b":3, "c":1},
{"a":1, "b":2, "c":3}
4
  • What are you trying to do, the end result you want isn't valid JSON. You'd need to wrap it in [..] if you want that to become valid.
    – Mendhak
    Commented Aug 28, 2015 at 9:42
  • I know, but I can do the wrapping manually.
    – Guforu
    Commented Aug 28, 2015 at 9:44
  • End of every line, or every but last line? Commented Aug 28, 2015 at 10:04
  • second, every but last
    – Guforu
    Commented Aug 28, 2015 at 11:20

6 Answers 6

3

You can use awk:

awk 'NR > 1{print line","}{line=$0;}END{print $0}' jsonfile
1
  • @chaos Corrected!!!
    – jcbermu
    Commented Aug 28, 2015 at 10:13
3

You can use sed:

sed '$n;s/$/,/' file
  • The $n; means if it's the last line just continue.
  • Else it adds a , at the end of the line.
  • If you add the -i flag (not POSIX) the file will be edited inplace.

If it must be awk:

awk 'NR>1{print p} {p=$0","}END{print}' file1
  • Stores the content of the line in a variable p.
  • The variable is printed, except in the last line.
  • When the input comes to an end, the last line is print as it is.
2

It seems that the real goal here is to produce a JSON array from a stream of JSON entities. A great tool for anything to do with JSON is

https://stedolan.github.io/jq/

The task of converting a file (say input.txt) of JSON entities to a JSON array is as simple as:

$ jq -s . input.txt

jq can also take input from STDIN.

1
  • 1
    This. jq is the swiss army knife for json, while most (all?) other answers fail when the task becomes more complex.
    – MCO
    Commented Feb 28, 2020 at 12:31
0

How about in perl:

#!/usr/bin/perl

use strict;
use warnings;

use JSON;

my $json_list; 
while ( my $json_str = <> ) { 
    push ( @$json_list, from_json ( $json_str ) );
}

print to_json ( $json_list, { pretty => 1, canonical => 1 } );

Takes data on STDIN or myscript.pl somefilename and takes the input you've designated, and builds a JSON array out of it.

Result:

[
   {
      "a" : 1,
      "b" : 1,
      "c" : 2
   },
   {
      "a" : 3,
      "b" : 3,
      "c" : 1
   },
   {
      "a" : 1,
      "b" : 2,
      "c" : 3
   }
]
0

The original file is actually Newline delimited JSON (NDJSON).

Instead of using GNU tools, I recommend installing json from NPM.

# say the file is test.log
$ json -f test.log -g
[
  {
    "a": 1,
    "b": 1,
    "c": 2
  },
  {
    "a": 3,
    "b": 3,
    "c": 1
  },
  {
    "a": 1,
    "b": 2,
    "c": 3
  }
]
0

Pure bash:

while read line
do
    echo "$line,"
done < <(head -n-1 testfile)
echo `tail -n1 testfile`

This script appends ',' to every line except the last, and then prints the last line unchanged.

4
  • Whats with the last comma? It should not be printed
    – chaos
    Commented Aug 28, 2015 at 10:03
  • Description says "to the end of every line" Commented Aug 28, 2015 at 10:03
  • But, the example shows different and it is not valid json
    – chaos
    Commented Aug 28, 2015 at 10:05
  • Okay then. Updated my answer to be a valid one. Commented Aug 28, 2015 at 10:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .