nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Alphabetize text within a file

This is a discussion on Alphabetize text within a file within the Shell scripting forums, part of the Development/Scripting category; Hello Forum people. I have been searching the Internet for the answer to my question, and I think it may ...

Register free or login to your existing account and remove all advertisements.


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2009, 09:31 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 525
Thanks: 54
Thanked 66 Times in 59 Posts
Rep Power: 8
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Alphabetize text within a file

Hello Forum people. I have been searching the Internet for the answer to my question, and I think it may be related to the command "sort" or "uniq"

In Example. I have a file with lines of text that are not in alphabetical order.

Code:
shine:~ jaysunn$ cat file1 
asdkfokaosfknksdigjisjigjdigjmnh

ojdfojjjojddfpdfo sdfokpoopsdf jsdfojsdf

wiewieiemd iuh asdlk  this is a file blah blah

shine:~ jaysunn$
What I would like to have happen is. I want to issue a command that will take the text within file1 and put it in alphabetical order from a-z. I am pretty sure that sort or uniq can achieve this. I have tried this and was unsuccessful. The command would alphabetize the text and output file2.

Code:
shine:~ jaysunn$ sort file1 | uniq -c > file2
   4 
   1 asdkfokaosfknksdigjisjigjdigjmnh
   1 ojdfojjjojddfpdfo sdfokpoopsdf jsdfojsdf
   1 wiewieiemd iuh asdlk  this is a file blah blah
shine:~ jaysunn$
Thanks in advance.

Jaysunn

Last edited by nixcraft; 07-02-2009 at 02:51 PM.
Reply With Quote
  #2 (permalink)  
Old 06-26-2009, 09:57 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Posts: 2,450
Thanks: 11
Thanked 189 Times in 139 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

You would like to sort it based upon first character of each line?
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 06-27-2009, 03:48 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 525
Thanks: 54
Thanked 66 Times in 59 Posts
Rep Power: 8
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Hey Nixcraft,
Thanks for the quick reply.


Yes my goal would be to alphabetize each string in the file in alphabetical order.

e.g.

Code:
ehqukldkg
hiuthhhtilc
To become
Code:
dehkklqu
chhhhhiiltu
Etc.......

Thanks as always for your forum and professionalism.

Jaysunn
Reply With Quote
  #4 (permalink)  
Old 06-27-2009, 04:33 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Posts: 2,450
Thanks: 11
Thanked 189 Times in 139 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try the following:

PHP Code:
#!/bin/bash
INPUT=$1

usage
(){
    echo 
"Usage: $0 input.txt"
    
exit 1
}

die(){
    echo 
"$@"
    
exit 2
}
[ $
# -ne 2 ] && usage

[ ! -"$INPUT" ] && die "File $INPUT does not exist!"

while read line 
do
    echo 
"$line" grep -. | sort -|tr -'\n'; echo ""
done "$INPUT" 
Quote:
Thanks as always for your forum and professionalism.
You're welcome!
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help

Last edited by nixcraft; 07-02-2009 at 02:46 PM. Reason: See below
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
jaysunn (06-27-2009)
  #5 (permalink)  
Old 06-27-2009, 07:35 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 525
Thanks: 54
Thanked 66 Times in 59 Posts
Rep Power: 8
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default [solved]

Thanks Nixcraft....
It works like a charm.

Jaysunn
Reply With Quote
  #6 (permalink)  
Old 07-02-2009, 10:09 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 71
Thanks: 0
Thanked 11 Times in 11 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by nixcraft View Post
Try the following:

Code:
#!/bin/bash
INPUT=$1

usage(){
    echo "Usage: $0 input.txt"
    exit 1
}

die(){
    echo "$@"
    exit 2
}
[ $# -ne 2 ] && usage

[ ! -f $INPUT ] && die "File $INPUT does not exist!"

That will fail if $INPUT contains spaces or is empty.

Code:
[ ! -f "$INPUT" ] && die "File $INPUT does not exist!"
Quote:
Code:
while read line 
do
    echo "$line" | grep -o . | sort -n |tr -d '\n'; echo ""

That can fail because most versions of grep so not have an -o option. Use
sed instead.

Code:
echo "$line" | sed 's/./&\
/g' | sort | tr -d '\n'; echo ""
(sort doesn't need the -n option.)
Quote:
Code:
done < $INPUT

That will fail if $INPUT contains spaces.

Code:
done < "$INPUT"
Reply With Quote
  #7 (permalink)  
Old 07-02-2009, 12:45 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Posts: 2,450
Thanks: 11
Thanked 189 Times in 139 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
That will fail if $INPUT contains spaces or is empty.
[ ! -f "$INPUT" ] && die "File $INPUT does not exist!"
How?
Code:
INPUT="this is a test.txt"
[ ! -f "$INPUT" ] && echo "No" || echo "Yes"
No
Code:
>"$INPUT"
[ ! -f "$INPUT" ] && echo "No" || echo "Yes"
Yes
And with empty
Code:
INPUT=""
 [ ! -f "$INPUT" ] && echo "No" || echo "Yes"
No

Tested on GNU bash, version 3.2.39.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #8 (permalink)  
Old 07-02-2009, 01:35 PM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 71
Thanks: 0
Thanked 11 Times in 11 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by nixcraft View Post
How?
Code:
INPUT="this is a test.txt"
[ ! -f "$INPUT" ] && echo "No" || echo "Yes"
No
Code:
>"$INPUT"
[ ! -f "$INPUT" ] && echo "No" || echo "Yes"
Yes
And with empty
Code:
INPUT=""
 [ ! -f "$INPUT" ] && echo "No" || echo "Yes"
No

Tested on GNU bash, version 3.2.39.

Now try it the way you wrote the script, without quotes around $INPUT.

It will give you error messages.
Reply With Quote
The Following User Says Thank You to cfajohnson For This Useful Post:
nixcraft (07-02-2009)
  #9 (permalink)  
Old 07-02-2009, 02:45 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Posts: 2,450
Thanks: 11
Thanked 189 Times in 139 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by cfajohnson View Post

Now try it the way you wrote the script, without quotes around $INPUT.

It will give you error messages.
Oh yes.. my bad, I should have tested it
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply

Tags
linux sort each character , shell scripting , shell sort each character , sort , sort each character shell , tr command , unix sort each character


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
To find and replace in text file nashtech Shell scripting 5 01-31-2010 07:32 AM
Shell Script To change strings / text in a text file jaysunn Shell scripting 1 05-08-2009 06:58 PM
Modify Text in a file maxcell Shell scripting 3 10-25-2008 09:33 PM
rearranging columns in a text file sureshbup Shell scripting 2 12-06-2006 10:43 AM
Replacing text in a file using awk postyrus Shell scripting 4 05-02-2005 04:31 PM


All times are GMT +5.5. The time now is 02:59 PM.


Powered by vBulletin® Version 3.8.4 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38