[bash] sorting files from 4 directories and merging


Recommended Posts

hey guys wondering if you could help me, I'm trying to create a script that will allow me to sort 4 files into one ordered list.

so I have a files and folders set up like this:

results (dir) --|---dir1 (dir) -- urls(file)

---------------------|---dir2 (dir) -- urls(file)

---------------------|---dir3 (dir) -- urls(file)

merge(file)

so i want to go through all dirs and get the text and sort them into one file in the results dir

the urls file will be coded as username firstname secondname userlevel lastfilesaved

i.e fblade Matthew Deloughry A bob.txt

so how can i get all the information from the urls file, order them by userlevel, then 2nd name, then by 1st name and then store it into a file called merge.

Link to comment
Share on other sites

surely its just of case of sorting the 1st url file then oupting that to a temp file then move onto the next url file and outputting that into another temp and so on. I'd have a crack my self but i dont know how to sort by one thing then another i know sort -k 4b dir1/urls would sort by the level access but i dont know how to then move onto the next sort criteria

Link to comment
Share on other sites

You could use cut to reorder the fields as "username secondname firstname userlevel lastfilesaved" and then sort away without specifying sort fields.

would that allow me to just type sort dir1/urls and that would sort it that way?

also will execution time be longer doing it that way due to have to use more commands?

Link to comment
Share on other sites

To be honest I would like to execution/ file size down as much as possible as i'll be running the script on my sever from another machine and want to keep time down as much as possible i know it may not be much but... I'm pedantic about this :p

edit

also if it did come to crunch and had to do it using cut how would it be possible if i had many lines in one file?

Link to comment
Share on other sites

You can process each line with a "for i in $(cat file.txt)" loop. Use cut to get the fields and output them together to another file.

Repeat this for all the urls in every dir, sort the file you used as output and do another for loop to put lines back to their original format.

Then again it might be easier to use something like awk or sed. I don't know much about their usage but they are supposed to be powerful tools for text processing.

Link to comment
Share on other sites

ok so i sorted this by using

awk '/^/ {print $4,$3,$2,$1,$5}' dir1/urls > temp
awk '/^/ {print $4,$3,$2,$1,$5}' dir2/urls >> temp
....... 
sort temp> temp2
awk '/^/ {print $4,$3,$2,$1,$5}' temp2 > final

but now how can i do it so only the person show once but his/her downloads are all next to the name? cut down size really

Link to comment
Share on other sites

Ok won't let me edit my post again I do not know why?

Anyways I've been playing and I could use join but I seem to get a problem in the fact that how can I Join say "fblade1987 matthew deloughry A bot.txt" and "fblade1987 matthew deloughry A severkill.txt" without it showing up as "fblade1987 matthew deloughry A bot.txt matthew deloughry A serverkill.txt "

Link to comment
Share on other sites

Ok so now used what you said but a question is there a simple way i can input say username, times d/l file1,times d/l file2,times d/l file3,times d/l file4, then average?

i have

awt ' {sum= ($2 + $3 + $4 + $5)/4
print $1,$2,$3,$4,$5, sum}'  final > times

i know that theres printf but not sure how to get it so its all on the same line and to show it rounded? can anyone help?

Link to comment
Share on other sites

I'm not sure I understood that right, it might be something like this:

echo "$1 $2 $3 $4 $5 $(echo scale=0\; $[$2 + $3 + $4 + $5] '/' 4 |bc )"

0 in "scale=0" being the decimal precision for bc.

Link to comment
Share on other sites

thanks for the reply i'll try this but see if i can clear up what i meant about the post.

all I want to do now is to create an average from the four download times i know in awt you do it something like that! its just when i do that it only produces the number to 2 decimal places i just want it to be rounded.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.