I have an input file that is text, consisting of lines that look like:


I need to split each line into fields at the | (vertical bar) characters and output each field with a label; i.e., produce lines of the form:

{ "IMSI":"352", "Status":"C", "ServiceType":"PAID", "Number":"7036", "ConnectionType":"VOICE", "ActivationDate":"01-FEB-12" }

How can I do this?

  • there are solutions using only the shell with arrays, but its much simpler and cleaner to use an awk script: awk -F '|' '{print "{\"IMSI\":\""$1"\" ... and so forth}'< input – ikrabbe Jun 25 '15 at 5:11
  • I found your question to be somewhat unclear.  I have made a guess at what you meant, and, happily, my guess seems to be the same as ikrabbe's.  I edited the question based on my understanding of (my guess at) its meaning.  If I changed it too much, please edit it and change it back.  (But, please, don't use nine- and fifteen-digit numbers unless you need to, and don't show six lines of input becoming one line of output unless you mean for the other five lines to be deleted.) – G-Man Says 'Reinstate Monica' Jun 25 '15 at 5:47

To do it entirely in the shell:

while IFS="|" read -r im st se nu co ac
    printf '{ "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s" }\n' \
        IMSI           "$im" \
        Status         "$st" \
        ServiceType    "$se" \
        Number         "$nu" \
        ConnectionType "$co" \
        ActivationData "$ac"
done < input > output

With a bit of Python, for a change:

#!/usr/bin/env python

import csv
import json
import sys
from collections import OrderedDict

keys = ['IMSI', 'Status', 'ServiceType', 'Number', 'ConnectionType', 'ActivationData']

with open(sys.argv[1], 'rt') as csvfile:
    reader = csv.reader(csvfile, delimiter='|')
    for row in reader:
        print json.dumps(OrderedDict(zip(keys, row)))


{"IMSI": "352", "Status": "C", "ServiceType": "PAID", "Number": "7036", "ConnectionType": "VOICE", "ActivationData": "01-FEB-12"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.