Allocating names to folders based on a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Allocating names to folders based on a file
# 1  
Old 04-28-2016
Question 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

Code:
[
	{"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...
Code:
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....
# 2  
Old 04-28-2016
Any attempt/idea/thought from your side?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-28-2016
Howsoever, try
Code:
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.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-28-2016
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 ...

Code:
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

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


Last edited by A-V; 04-28-2016 at 02:31 PM..
# 5  
Old 04-28-2016
Not sure I understand your problem. If the original file still exists, try
Code:
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

This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-28-2016
the data i originally posted at a mistake as I tried to cut it short
original
Code:
[
	{"gender": "f",  "age": "15",   	"folder": "data1"},
	{"gender": "f",  "age": "15",   	"folder": "data2"},
	{"gender": "m"  "age": "15",  	"folder": "data3"},
]

what it should like is
Code:
[
	{"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 "

Code:
mv: cannot stat ‘data8\r’: No such file or directory


Last edited by A-V; 04-28-2016 at 04:25 PM..
# 7  
Old 04-29-2016
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 fora as well.

Last edited by RudiC; 04-29-2016 at 04:07 AM..
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort a text file based on names in square brackets

Hi all, I have a text file similar to this: Text More text Etc Stuff That Is Needed Etc Etc This contains over 70 entries and each entry has several lines of text below the name in square brackets. (5 Replies)
Discussion started by: Scally
5 Replies

2. UNIX for Beginners Questions & Answers

Consternation of multiple file names based on naming pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

3. Shell Programming and Scripting

Sort html based on .jar, .war file names and still keep text within three groups.

Output from zipdiff GNU EAR comparison tool produces output in html divided into three sections "Added, Removed, Changed". I want the output to be sorted by jar or war file. <html> <body> <table> <tr> <td class="diffs" colspan="2">Added </td> </tr> <tr><td> <ul>... (5 Replies)
Discussion started by: kchinnam
5 Replies

4. Shell Programming and Scripting

Files with same names in different folders

Hello, I am looking for a command line that can do some operations on two files that have the same names but in different folders. for example if folder A contains files 1.txt, 2.txt, 3.txt,.. folder B contains files 1.txt, 2.txt, 3.txt,.. If I would like to concatenate the two files... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

5. OS X (Apple)

Remove leading spaces from file names and folders

Hi All, I have a vexing issue with leading spaces in file names. Basically, we're moving tons of data from our ancient afp file share to Box.com and Box forbids leading spaces in files or folders. The HFS file system seems to be perfectly fine with this, but almost all other Unix file systems... (1 Reply)
Discussion started by: prometheon123
1 Replies

6. Shell Programming and Scripting

Ordering Folders having Date as Names

Hi All, I have directories under /development/arun/weekly/ 20120421 20120414 . . . . I need to arrange these directories in descending order. folder name with recent date will be on top and then others. (1 Reply)
Discussion started by: Arun Mishra
1 Replies

7. Shell Programming and Scripting

Sorting file based on names

Hi I have some files in directory and the names of files are like jnhld_15233_2010-11-23 jnhld_15233_2007-10-01 jnhld_15233_2001-05-04 jnhld_15233_2011-11-11 jnhld_15233_2005-06-07 jnhld_15233_2000-04-01 ..etc How can i sort these files based on the date in the file name so that ... (4 Replies)
Discussion started by: morbid_angel
4 Replies

8. Shell Programming and Scripting

Archive different folders based on their names

This is my first post so ... be gentle:) Hello I have several folders that are backed up daily in following format: /back_YY.MM.DD/backup1/* ........................./backup2/* I looking a script to archive and rename all backup folders bazed on root folder... (8 Replies)
Discussion started by: vilibit
8 Replies

9. Shell Programming and Scripting

Separate based on file names

Hello experts, This might prove to be a stupid question to some of you, but I have tried to tackle it in different ways. Being new to shell scripting, I am requesting your help in coming up with an elegant solution. I am using Korn shell. We have a directory with file names with the pattern:... (2 Replies)
Discussion started by: prashk15
2 Replies

10. UNIX for Dummies Questions & Answers

File names based off of wildcards

Hello all- First post, so just to forewarn you: I know enough about Perl and the Terminal to get myself into trouble, not quite enough to always get out. I'd like to know if it is possible to, from the command prompt, use a wild-card to declare the names of files for input and then use the... (1 Reply)
Discussion started by: tcquad
1 Replies
Login or Register to Ask a Question