1

I've got a peace of large text file with readings like below,

name=ABC
class=3
age=7
roll_no=41 

name=XYZ
class=4
age=9
roll_no=23 

So, how can I separate each name with their respective age and write the result in a single line, values separated by a space, like this

ABC 3
XYZ 9

Is there any tool/script to save the result in JSON format ?

Tried hours with awk, sed, tr, grep etc. etc. but I'm horrible at command line text processing, thanks in advance.

2
  • 1
    What should your JSON output look like? Like this: [{"ABC":7},{"XYZ":9}] ? Sep 30, 2016 at 10:12
  • @glenn, yeah, that'll be fine. or even simpler like this '{"ABC":7,"XYZ":9}' , I'll parse it with jq .
    – Arnab
    Oct 3, 2016 at 13:10

4 Answers 4

2

I'd use awk:

awk -F"=" '
    {data[$1] = $2} 
    function output() {
        if ("name" in data && "age" in data) 
            print data["name"], data["age"]
        delete data
    }
    NF == 0 {output()} 
    END     {output()}
' filename
2
  • That's perfect, I could also modify the output easily, working like a charm with busybox inbuilt awk . Thank U !
    – Arnab
    Oct 1, 2016 at 2:22
  • Sir, could you explain me a bit what's happening step by step ? Thanks !
    – Arnab
    Oct 3, 2016 at 13:17
1

If perl is available, chunks of text separated by one or more blank lines are perfect for processing with perl's paragraph mode:

perl -n00e ' # Read the input file paragraph-by-paragraph
  # Split each paragraph into lines and join the lines with an = sign:
  $s = join "=",split /\n/;
  # Split the joined fields on = signs (with possible blanks around them):
  %h = split /\s*=\s*/,$s;
  print "$h{name} $h{age}\n" # Print the required information
' your_file

In the above, the code parses your file into the hash structure (%h) where the keys are the field names (on the left of the = signs) and the values are the field values (on the right of the = signs) on each line. Once the paragraph has been parsed into this structure, we print out the values of the "name" and "age" fields.

1
  • Thanks, working perfectly !! Wrote a little shell script for easier parsing, but is there any other solution without using perl ?? I want to use something similar in OpenWrt.
    – Arnab
    Sep 30, 2016 at 8:50
1

You can use below command:

grep -E "name|age" filename | sed 'N;s/\n/ /' | sed 's/[=,]/ /g'| awk '{ print $2, $4}'

it will give output like :

ABC 7
XYZ 9

EDIT:

Below command will give line which have string name or age

grep -E "name|age" filename

name=ABC
age=7
name=XYZ
age=9

This command will make one line of two lines

grep -E "name|age" filename | sed 'N;s/\n/ /'
name=ABC age=7
name=XYZ age=9

Below command will remove equal = sign from output

grep -E "name|age" filename | sed 'N;s/\n/ /' | sed 's/[=,]/ /g'
name ABC age 7
name XYZ age 9

Below command printing your requiremnt

grep -E "name|age" filename | sed 'N;s/\n/ /' | sed 's/[=,]/ /g'| awk '{ print $2, $4}'
ABC 7
XYZ 9

awk printing 2nd and 4th argument of previous command output

still you are facing any problem to understand any syntax google may explain in deep :) .

2
  • Thanks, it's working too, but a bit difficult to understand(for me) what's going on.
    – Arnab
    Oct 1, 2016 at 2:25
  • Thanks a lot, now I can understand it better, now I've to learn sed basics.
    – Arnab
    Oct 3, 2016 at 13:14
0

Since you asked for another language. Here's one way to do it in Python, saving the input in a dictionary so that you can easily access the values with the keys name and age:

#!/usr/bin/env python3

myDict = {}

with open("ages") as inFile:
    for line in inFile:
       if line == "\n":
            print(myDict['name'].rstrip() + " " + myDict['age'], end="")
            continue
       (key, value) = line.split("=")
       myDict[key] = value

print(myDict['name'].rstrip() + " " + myDict['age'], end="")

Given this infile (ages):

name=ABC
class=3
age=7
roll_no=41

name=XYZ
class=4
age=9
roll_no=23

I get this result:

./pyAges.py 
ABC 7
XYZ 9
2
  • Thanks a lot, but python a bit heavy on embedded systems, I prefer to use CLI tools.
    – Arnab
    Oct 1, 2016 at 2:05
  • @Arnab No problem. You should of course use the solution that is best for you.
    – anon
    Oct 1, 2016 at 5:29

You must log in to answer this question.

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