nixCraft Linux / UNIX / Shell Scripting 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 account to remove all advertisements.

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

Linux answers from nixCraft.


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

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 26th June 2009, 08:31 PM
jaysunn's Avatar
Contributors
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: Red Hat Linux
Scripting language: bash awk sed
Posts: 793
Thanks: 116
Thanked 107 Times in 97 Posts
Rep Power: 14
jaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud of
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; 2nd July 2009 at 01:51 PM.
Reply With Quote
  #2 (permalink)  
Old 26th June 2009, 08:57 PM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,300
Thanks: 13
Thanked 413 Times in 306 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond repute
Default

You would like to sort it based upon first character of each line?
__________________
Vivek Gite
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog
Reply With Quote
  #3 (permalink)  
Old 27th June 2009, 02:48 AM
jaysunn's Avatar
Contributors
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: Red Hat Linux
Scripting language: bash awk sed
Posts: 793
Thanks: 116
Thanked 107 Times in 97 Posts
Rep Power: 14
jaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud of
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 27th June 2009, 03:33 AM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,300
Thanks: 13
Thanked 413 Times in 306 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft 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
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog

Last edited by nixcraft; 2nd July 2009 at 01:46 PM. Reason: See below
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
jaysunn (27th June 2009)
  #5 (permalink)  
Old 27th June 2009, 06:35 PM
jaysunn's Avatar
Contributors
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: Red Hat Linux
Scripting language: bash awk sed
Posts: 793
Thanks: 116
Thanked 107 Times in 97 Posts
Rep Power: 14
jaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud ofjaysunn has much to be proud of
Default [solved]

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

Jaysunn
Reply With Quote
  #6 (permalink)  
Old 2nd July 2009, 09:09 AM
Senior Member
 
Join Date: May 2009
OS: Mandriva
Posts: 110
Thanks: 0
Thanked 29 Times in 23 Posts
Rep Power: 5
cfajohnson is a jewel in the roughcfajohnson is a jewel in the roughcfajohnson is a jewel in the roughcfajohnson is a jewel in the rough
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 2nd July 2009, 11:45 AM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,300
Thanks: 13
Thanked 413 Times in 306 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft 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
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog
Reply With Quote
  #8 (permalink)  
Old 2nd July 2009, 12:35 PM
Senior Member
 
Join Date: May 2009
OS: Mandriva
Posts: 110
Thanks: 0
Thanked 29 Times in 23 Posts
Rep Power: 5
cfajohnson is a jewel in the roughcfajohnson is a jewel in the roughcfajohnson is a jewel in the roughcfajohnson is a jewel in the rough
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 (2nd July 2009)
  #9 (permalink)  
Old 2nd July 2009, 01:45 PM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,300
Thanks: 13
Thanked 413 Times in 306 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft 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
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog
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 6 6th September 2010 10:05 AM
Shell Script To change strings / text in a text file jaysunn Shell scripting 1 8th May 2009 05:58 PM
Modify Text in a file maxcell Shell scripting 3 25th October 2008 08:33 PM
rearranging columns in a text file sureshbup Shell scripting 2 6th December 2006 09:43 AM
Replacing text in a file using awk postyrus Shell scripting 4 2nd May 2005 03:31 PM


All times are GMT +5.5. The time now is 01:07 PM.


Powered by vBulletin® Version 3.8.6 - 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 39 40