Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Sort a text file based on names in square brackets Post 303024627 by jgt on Friday 12th of October 2018 07:43:11 PM
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#

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Text::PDF::TTFont(3pm)					User Contributed Perl Documentation				    Text::PDF::TTFont(3pm)

NAME
Text::PDF::TTFont - Inherits from Text::PDF::Dict and represents a TrueType font within a PDF file. DESCRIPTION
A font consists of two primary parts in a PDF file: the header and the font descriptor. Whilst two fonts may share font descriptors, they will have their own header dictionaries including encoding and widhth information. INSTANCE VARIABLES
There are no instance variables beyond the variables which directly correspond to entries in the appropriate PDF dictionaries. METHODS
Text::PDF::TTFont->new($parent, $fontfname, $pdfname, %opts) Creates a new font resource for the given fontfile. This includes the font descriptor and the font stream. The $pdfname is the name by which this font resource will be known throught a particular PDF file. All font resources are full PDF objects. $t->width($text) Measures the width of the given text according to the widths in the font $t->trim($text, $len) Trims the given text to the given length (in per mille em) returning the trimmed text $t->out_text($text) Indicates to the font that the text is to be output and returns the text to be output $f->copy Copies the font object excluding the name, widths and encoding, etc. TITLE
Text::PDF::TTIOString - internal IO type handle for string output for font embedding. This code is ripped out of IO::Scalar, to save the direct dependence for so little. See IO::Scalar for details perl v5.8.8 2006-09-09 Text::PDF::TTFont(3pm)
All times are GMT -4. The time now is 08:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy