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 ...


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 26-06-2009, 08:31 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
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 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; 02-07-2009 at 01:51 PM.
Reply With Quote
  #2 (permalink)  
Old 26-06-2009, 08:57 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 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 27-06-2009, 02:48 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
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 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 27-06-2009, 03:33 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 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; 02-07-2009 at 01:46 PM. Reason: See below
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
jaysunn (27-06-2009)
  #5 (permalink)  
Old 27-06-2009, 06:35 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
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 jaysunn is a splendid one to behold
Default [solved]

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

Jaysunn
Reply With Quote
  #6 (permalink)  
Old 02-07-2009, 09:09 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 93
Thanks: 0
Thanked 16 Times in 16 Posts
Rep Power: 3
cfajohnson has a spectacular aura about 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 02-07-2009, 11:45 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 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 02-07-2009, 12:35 PM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 93
Thanks: 0
Thanked 16 Times in 16 Posts
Rep Power: 3
cfajohnson has a spectacular aura about 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 (02-07-2009)
  #9 (permalink)  
Old 02-07-2009, 01:45 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 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 31-01-2010 06:32 AM
Shell Script To change strings / text in a text file jaysunn Shell scripting 1 08-05-2009 05:58 PM
Modify Text in a file maxcell Shell scripting 3 25-10-2008 08:33 PM
rearranging columns in a text file sureshbup Shell scripting 2 06-12-2006 09:43 AM
Replacing text in a file using awk postyrus Shell scripting 4 02-05-2005 03:31 PM


All times are GMT +5.5. The time now is 06:52 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 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