Allocating names to folders based on a file

Hi everyone,
I have a problem and I would be gratful if you can help.

I have set of folders with files in them. e.g. data1, data2, data3
and I have a json file with info ... looking like this

[
	{"gender": "f",  "age": "15",   	"folder": "data1"},
	{"gender": "f",  "age": "15",   	"folder": "data2"},
	{"gender": "m"  "age": "15",  	"folder": "data3"},
]

I want to rename my files to replace the data with their gender to some processing and back to the real name later on...

f1
f2
m3

Do i need to convert the json file first? perl or awk?
or can data just be extracted (using awk?) and then folders changed based on that?

Thank you for your help....

Any attempt/idea/thought from your side?

1 Like

Howsoever, try

awk '/gender/ {gsub (/[        "}]*/, _); T = $2 $6; sub ("data", _, T); print "echo mv " $6 " " T}' FS="[:,]" file | sh
mv data1 f1
mv data2 f2
mv data3 m3

Remove the "echo" if happy with the result.

1 Like

Thank you RudiC,
I realised that I have a comma more by mistake at the end of last line but the comma is next to "m". So my final like it getting messy ...

it should be 
	{"gender": "m",  "age": "15",  	"folder": "data3"}

the results that i get for the last line is 
,m3data3

one question ... how would you reverse this considering the other way around there are three options (m,f, NA)
I tried with f only but it does not detect the pattern

awk '/gender/ {gsub (/[        "}]*/, _); T = $6 $2; sub ("f", _, T); print " echo mv " $2 " " T}' FS="[:,]" detail.json | sh

Not sure I understand your problem. If the original file still exists, try

awk '/gender/ {gsub (/[        "}]*/, _); T = $2 $6; sub ("data", _, T); print "echo mv " T " " $6}' FS="[:,]" file | sh
mv f1 data1
mv f2 data2
mv m3 data3
1 Like

the data i originally posted at a mistake as I tried to cut it short
original

[
	{"gender": "f",  "age": "15",   	"folder": "data1"},
	{"gender": "f",  "age": "15",   	"folder": "data2"},
	{"gender": "m"  "age": "15",  	"folder": "data3"},
]

what it should like is

[
	{"gender": "f",  "age": "15",   	"folder": "data1"},
	{"gender": "f",  "age": "15",   	"folder": "data2"},
	{"gender": "m",  "age": "15",  	"folder": "data3"}
]

now it works magic on all the lines but the last like give me an error..." \r "

mv: cannot stat �data8\r�: No such file or directory

Which is an indicator for an Non-*nix-text, probably created by an MS application. Try - if available - dos2unix to convert your input file. Other means have been published in these forums as well.

1 Like

Not sure if this works for you: replace gsub (/[ "}]*/, _) with gsub (/[ \r"}]*/, _) . Be aware that the space in the search pattern is a <space> plus a <TAB> char!

1 Like

Thank you very much RudiC for the great help .. it was the doc2unix problem.
have the command but it was only converting txt and csv