LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-22-2009, 09:32 AM   #1
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Rep: Reputation: 15
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,
 
Old 10-22-2009, 10:44 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Rep: Reputation: 99
Lightbulb

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.

Let me check if in case I can find something useful for you.
 
Old 10-22-2009, 09:54 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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
 
Old 10-23-2009, 04:59 AM   #4
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thumbs up

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

Thank you to both of you ghostdog74 & vikas027
 
Old 10-23-2009, 06:07 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Rep: Reputation: 99
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 10-23-2009, 06:55 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if formatting needs to be done, it can done using printf with left/right justification format specifiers, eg %10s, %-15s etc...
 
Old 10-23-2009, 07:57 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Rep: Reputation: 99
Question

Quote:
Originally Posted by ghostdog74 View Post
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.

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).
 
Old 10-23-2009, 08:23 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by vikas027 View Post
I tried printf function But could not succeed.
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
 
Old 11-04-2009, 06:57 AM   #9
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
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,
 
Old 11-04-2009, 07:07 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by keenboy View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help stripping statement from text file, ksh: sed awk? austin881 Programming 7 07-13-2009 04:31 PM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 05:54 PM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 07:35 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
/etc/rc.sysinit: /bin/awk: Text file busy teeno Linux - Software 5 02-23-2005 02:19 AM


All times are GMT -5. The time now is 11:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration