0

I have a input file called usernames.txt:

user1 group1 group2
user2 group1
user3

What I need to do is take each line, make it an array and with the first element (user) create a file called user.json. I then need to take the remaining elements in the array and use them for the user groups.

The output file(s) should look like this:

{
  "id": "user1",
  "groups": "group1, group2",
  "shell": "\/bin\/bash",
  "password": "!"
}

What I have so far:

cat usernames.txt | tr ' \t' '\n\n' | while read user; do
    if test -f "$user.json"; then
       echo "Skipping \"$user\", it already exists"
    else
      arr=$user
      cat > ${arr[0]}.json << EOF
{
  "id": "${arr[0]}",
  "groups": "${arr[@]:1}",
  "shell": "\/b=in\/bash",
  "password": "!"
}
EOF
    fi
done  

I am not sure how to handle the comma between groups, but I'm sure that's not too difficult.

What I'm currently getting for output:

Skipping "group1", it already exists

group1.json group2.json user1.json user2.json user3.json

It is creating files for the groups which I don't want...

user1.json:

{
  "id": "user1",
  "groups": "ser1",
  "shell": "\/bin\/bash",
  "password": "!"
}

It also seems each array element is a letter instead of the whole word? I'm sure it has something to do with how I create the array, but I'm not sure beyond that.

Any ideas how to get this working?

shlant
  • 71
  • 3
  • 9
  • http://stackoverflow.com/questions/12524437/output-json-from-bash-script possible duplicate – V H Apr 16 '14 at 15:41
  • that is using separate variables. I am working with arrays – shlant Apr 16 '14 at 15:50
  • You have provided everything besides the essential fully working example of usernames.txt since the output does not match provided usernames.txt at the very top – V H Apr 16 '14 at 15:55
  • sorry, you're saying I'm providing the wrong input for the output I want? – shlant Apr 16 '14 at 16:02

1 Answers1

0

I am not going to try answer your concerns on the above comments since it will take for ever:

Have a mess around with below awk statement:

awk '{print "{\n" "id" ": "$1",\n" "group: " $2 ",\nShell:  /bin/bash ,\n Password: \!,\n}\n"}' usernames.txt 
awk: cmd. line:1: warning: escape sequence `\!' treated as plain `!'
{
id: user1,
group: group1,
Shell:  /bin/bash ,
 Password: !,
}

{
id: user2,
group: group1,
Shell:  /bin/bash ,
 Password: !,
}

{
id: user3,
group: ,
Shell:  /bin/bash ,
 Password: !,
}

The problem is in your question you state:

I have a input file called usernames.txt:

user1 group1 group2
user2 group1
user3

What I need to do is take each line, make it an array and with the first element (user) create a file called user.json. I then need to take the remaining elements in the array and use them for the user groups.

The output file(s) should look like this:

{
  "id": "user1",
  "groups": "group1, group2",
  "shell": "\/bin\/bash",
  "password": "!"
}

your usernames.txt does not include shell or password fields and yet results does, so above awk assumes all entries will be like your example rather than defined in username.txt

You will need to fiddle around to add "around fields"

also in last example had no group so you may wish to add if ($2 ~ "" ) { do nothing }

within awk statement

V H
  • 8,382
  • 2
  • 28
  • 48