LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to rearrange a text file with awk (https://www.linuxquestions.org/questions/programming-9/how-to-rearrange-a-text-file-with-awk-763723/)

keenboy 10-22-2009 09:32 AM

how to rearrange a text file with awk
 
Hi,

I have a text file which is created by the output of a du command and it looks like this:

Code:

127M        /home/jackieayres/Maildir
108K        /home/jackieayres/My Folders
146M        /home/jackiecarr/Maildir
8.0K        /home/jamescarter/Desktop
4.0K        /home/jamescarter/My Folders
8.0K        /home/jasonmccairns/Desktop
4.0K        /home/jasonmccairns/My Folders
8.0K        /home/jillwarren/Desktop
4.0K        /home/jillwarren/My Folders
8.0K        /home/juliehamley/Desktop
4.5M        /home/juliehamley/My Folders
8.0K        /home/karenskyrme/Desktop
520K        /home/karenskyrme/My Folders
8.0K        /home/katigbeve/Desktop
108K        /home/katigbeve/My Folders
8.0K        /home/kellyhill/Desktop
484K        /home/kellyhill/Maildir
4.0K        /home/kellyhill/My Folders

What I'd like to do would be to get awk (or something else) to sort the username and put each of the same instance in a separate column.

The result I'd like would be:

Code:

127M        /home/jackieayres/Desktop  108K /home/jackieayres/My Folders
146M        /home/jackiecarr/Maildir
8.0K        /home/jamescarter/Desktop  4.0K /home/jamescarter/My Folders
8.0K        /home/jasonmccairns/Desktop 4.0K /home/jasonmccairns/My Folders
8.0K        /home/jillwarren/Desktop    4.0K /home/jillwarren/My Folders
8.0K        /home/juliehamley/Desktop  4.5M /home/juliehamley/My Folders

Some users have 3 folders which would mean the final file would have 3 columnm.

Is this something awk can do?

I looked at paste but I wasn't too sure what was best.

Thanks,

vikas027 10-22-2009 10:44 AM

Here, it is.

Code:

cat file | awk -F"/" '{print $3}' | uniq > names
for i in `cat names`;
do
grep $i file | sed 's/./ &/' | tr -d "\n" >> final
echo "" >> final
done;

Now, the output as per your example will be.
Code:

127M  /home/jackieayres/Maildir 108K  /home/jackieayres/My Folders
 146M  /home/jackiecarr/Maildir
 8.0K  /home/jamescarter/Desktop 4.0K  /home/jamescarter/My Folders
 8.0K  /home/jasonmccairns/Desktop 4.0K        /home/jasonmccairns/My Folders
 8.0K  /home/jillwarren/Desktop 4.0K  /home/jillwarren/My Folders
 8.0K  /home/juliehamley/Desktop 4.5M  /home/juliehamley/My Folders
 8.0K  /home/karenskyrme/Desktop 520K  /home/karenskyrme/My Folders
 8.0K  /home/katigbeve/Desktop 108K    /home/katigbeve/My Folders
 8.0K  /home/kellyhill/Desktop 484K    /home/kellyhill/Maildir 4.0K    /home/kellyhill/My Folders

Now, we just need to align the columns. I have done this before, but do not remember the commands. :o

Let me check if in case I can find something useful for you.

ghostdog74 10-22-2009 09:54 PM

you can do all that with just awk going through the file just ONCE.
Code:

#!/bin/bash
awk 'BEGIN{FS="/"}
{
  p="/"$2"/"$3
  if (! (p in a) ) {
    a[p]=$0
  }else{
    a[p]=a[p]" "$0
  }
}END{    for(i in a){print a[i] }}' file

output
Code:

$ ./shell.sh
8.0K    /home/jillwarren/Desktop 4.0K  /home/jillwarren/My Folders
8.0K    /home/katigbeve/Desktop 108K    /home/katigbeve/My Folders
127M    /home/jackieayres/Maildir 108K  /home/jackieayres/My Folders
8.0K    /home/jamescarter/Desktop 4.0K  /home/jamescarter/My Folders
8.0K    /home/jasonmccairns/Desktop 4.0K        /home/jasonmccairns/My Folders
8.0K    /home/karenskyrme/Desktop 520K  /home/karenskyrme/My Folders
146M    /home/jackiecarr/Maildir
8.0K    /home/juliehamley/Desktop 4.5M  /home/juliehamley/My Folders
8.0K    /home/kellyhill/Desktop 484K    /home/kellyhill/Maildir 4.0K    /home/kellyhill/My Folders


keenboy 10-23-2009 04:59 AM

Brilliant. Thanks very much now I have what I need and have my first script finished and work :D

Thank you to both of you ghostdog74 & vikas027

vikas027 10-23-2009 06:07 AM

Quote:

Originally Posted by ghostdog74 (Post 3729309)
output
Code:

$ ./shell.sh
8.0K    /home/jillwarren/Desktop 4.0K  /home/jillwarren/My Folders
8.0K    /home/katigbeve/Desktop 108K    /home/katigbeve/My Folders
127M    /home/jackieayres/Maildir 108K  /home/jackieayres/My Folders
8.0K    /home/jamescarter/Desktop 4.0K  /home/jamescarter/My Folders
8.0K    /home/jasonmccairns/Desktop 4.0K        /home/jasonmccairns/My Folders
8.0K    /home/karenskyrme/Desktop 520K  /home/karenskyrme/My Folders
146M    /home/jackiecarr/Maildir
8.0K    /home/juliehamley/Desktop 4.5M  /home/juliehamley/My Folders
8.0K    /home/kellyhill/Desktop 484K    /home/kellyhill/Maildir 4.0K    /home/kellyhill/My Folders


Ghostdog, how can we align in this text. Could you please help ?
Thanks in adv.

ghostdog74 10-23-2009 06:55 AM

if formatting needs to be done, it can done using printf with left/right justification format specifiers, eg %10s, %-15s etc...

vikas027 10-23-2009 07:57 AM

Quote:

Originally Posted by ghostdog74 (Post 3729715)
if formatting needs to be done, it can done using printf with left/right justification format specifiers, eg %10s, %-15s etc...

I tried printf function But could not succeed. :o

Is there something like I can define that any particular column, say column 3 starts from a particular column of screen say 35th (i.e. after 34 characters from left).

ghostdog74 10-23-2009 08:23 AM

Quote:

Originally Posted by vikas027 (Post 3729761)
I tried printf function But could not succeed. :o

its all in the manual..
Quote:

Is there something like I can define that any particular column, say column 3 starts from a particular column of screen say 35th (i.e. after 34 characters from left).
i don't usually care with formatting, but you can read this to see if its what you need

keenboy 11-04-2009 06:57 AM

Ghostdog74 I am just looking at your code again

Quote:

#!/bin/bash
awk 'BEGIN{FS="/"}
{
p="/"$2"/"$3
if (! (p in a) ) {
a[p]=$0
}else{
a[p]=a[p]" "$0
}
}END{ for(i in a){print a[i] }}' file
is there a way to sort the results alphabetically using awk?

Thanks,

ghostdog74 11-04-2009 07:07 AM

Quote:

Originally Posted by keenboy (Post 3744084)
is there a way to sort the results alphabetically using awk?
Thanks,

of course. you can use gawk's internal asort() or asorti() (check the gawk manual (my sig) and see how to use them, or just pipe the output to sort. simple?


All times are GMT -5. The time now is 03:01 PM.