Sort a text file based on names in square brackets


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Sort a text file based on names in square brackets
# 1  
Old 10-12-2018
Sort a text file based on names in square brackets

Hi all,

I have a text file similar to this:

Code:
[banana]
Text
More text
Etc

[apple]
Stuff
That
Is
Needed

[friend]
Etc
Etc

This contains over 70 entries and each entry has several lines of text below the name in square brackets.

I want to sort the whole file into a new file in alphabetical order based on the word in the square brackets and keep all text below it, up to the next name in square brackets.

So

Code:
[apple]
..
..

[banana]
..
..

[friend]
..
..

How can I do this please?

Thanks,

Al.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-12-2018 at 06:31 PM.. Reason: Added CODE tags.
# 2  
Old 10-12-2018
Welcome to the forum.


Any attempts / ideas / thoughts from your side?
# 3  
Old 10-12-2018
Code:
#!/usr/bin/bash                      
while read line                      
do                                   
if [ "${line:0:1}" = "[" ]           
then                                 
        len=${#line}                 
        (( len = $len - 2 ))         
        outfile=${line:1:$len}       
        echo "$line" >$outfile.txt   
else                                 
        echo "$line" >>$outfile.txt  
fi                                   
done <inputfile                      
 cat *.txt >true_outfile

I ran this in an empty directory except for the input file, and the script.
You may find whatever application you are using this in will be simpler if you keep the intermediate files rather than the final file.
Code:
-bash-3.2# l                                                        
total 48                                                            
-rwxr-xr-x    1 root     sys        232 Oct 12 19:37 i              
-rw-r--r--    1 root     sys         76 Oct 12 19:03 inputfile      
-rw-r--r--    1 root     sys         76 Oct 12 19:37 true_outfile   
-bash-3.2# ./i                                                      
-bash-3.2# l                                                        
total 96                                                            
-rw-r--r--    1 root     sys         30 Oct 12 19:40 apple.txt      
-rw-r--r--    1 root     sys         29 Oct 12 19:40 banana.txt     
-rw-r--r--    1 root     sys         17 Oct 12 19:40 friend.txt     
-rwxr-xr-x    1 root     sys        232 Oct 12 19:37 i              
-rw-r--r--    1 root     sys         76 Oct 12 19:03 inputfile      
-rw-r--r--    1 root     sys         76 Oct 12 19:40 true_outfile   
-bash-3.2#

# 4  
Old 10-13-2018
Try also

Code:
awk '$NF=$NF OFS' RS= ORS="\n" FS="\n" OFS="\t" file | sort | tr '\t' '\n'
[apple]
Stuff
That
Is
Needed

[banana]
Text
More text
Etc

[friend]
Etc
Etc


Last edited by RudiC; 10-13-2018 at 06:41 AM..
# 5  
Old 10-13-2018
Thanks very much guys, I’ll try later and report back.

Cheers,

Al.
# 6  
Old 10-14-2018
It works a treat, thanks guys

Al.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

IF statement with square brackets

Hi All, Hope you all are doing good. Yesterday in my project i came across a scenario which i can not guess why it was working in one region and why it was not in another region. Please find my issue below. I am using AIX version 6.0 of UNIX in my project, in shell scripting i have the... (1 Reply)
Discussion started by: mad man
1 Replies

2. Shell Programming and Scripting

Sort html based on .jar, .war file names and still keep text within three groups.

Output from zipdiff GNU EAR comparison tool produces output in html divided into three sections "Added, Removed, Changed". I want the output to be sorted by jar or war file. <html> <body> <table> <tr> <td class="diffs" colspan="2">Added </td> </tr> <tr><td> <ul>... (5 Replies)
Discussion started by: kchinnam
5 Replies

3. Shell Programming and Scripting

Grep number between Square [] brackets

I wanted to store the number inside the square bracket between colon( : ) and closing suqre bracket(]) in some variable. Suppose I have lines like : Input file : 20140320 00:08:23.846 INFO 84] - anything in line 20140320 00:08:23.846 Test 589] - Virtual and lab lab anything... (18 Replies)
Discussion started by: nes
18 Replies

4. Shell Programming and Scripting

Compare the value in between square brackets in file

I wanted to compare the value inside the Squre bracket after Colon ( : ) based on any value(seperated by or operator | ) inside the variable Thread and if match found then wnated to store in output file Input file : 20140320 00:08:43.918 INO 35] - Corporate hub is 20140320 00:08:43.918... (2 Replies)
Discussion started by: nes
2 Replies

5. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

6. Shell Programming and Scripting

Extract text between two square [..] brackets

Hi All, After searching about this, I could find some solutions but I am not sure why it is not working in my case. I have a text file with contents between two square brackets. The text file looks like this: Use tags when you post any code so others can easily read your code. You can... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

Replacing text between two square brackets

hi guys, i'm writing a script that looks for a unquie id in a file and replaces a string between two square brackets on the same line as the unquie id: ....... ....... 0001 zz 43242 replace this text] name 0002 sd 65466 UK] country ....... ....... how can i find line with id 0001... (6 Replies)
Discussion started by: zaff
6 Replies

9. Shell Programming and Scripting

WHy the double square brackets?

One of the senior administrators gave me a shell script to modify and it begins as follows: if ] && ] {more code follows} Why the double square brackets? (10 Replies)
Discussion started by: mojoman
10 Replies

10. UNIX for Dummies Questions & Answers

square brackets

I would like to substitute a phrase which contains square brackets. change TO how? Thanks (2 Replies)
Discussion started by: gilead29
2 Replies
Login or Register to Ask a Question