How to change the orders of the lines in a txt according to its length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change the orders of the lines in a txt according to its length
# 1  
Old 12-23-2011
How to change the orders of the lines in a txt according to its length

I have a txt:
Code:
a/b/c/d
a/b/c
b/f/g/v/m

I want ot change this txt according to the number of '/'

the result should be like:
Code:
b/f/g/v/m
a/b/c/d
a/b/c

how to do that?

Thanks in advance


I need shell...sorry, python or perl is a good solution, but i want shell
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 12-23-2011 at 01:05 PM.. Reason: code tags, please!
# 2  
Old 12-23-2011
Hi Henryyy,

One solution using Perl:

Code:
$ cat infile
a/b/c/d
a/b/c
b/f/g/v/m
$ cat script.pl
use warnings;
use strict;

@ARGV == 1 or die qq[Usage: perl $0 input-file\n];

my %line;

while ( <> ) {
        chomp;
        $line{ tr#/#/# } = $_;
}

for ( sort { $b <=> $a } keys %line ) {
        printf qq[%s\n], $line{ $_ };
}
$ perl script.pl infile
b/f/g/v/m
a/b/c/d
a/b/c

Regards,
Birei
# 3  
Old 12-23-2011
Perl one-liner:
Code:
perl -F"\n" -ln0ae '$,="\n";print sort {$x=$a;$y=$b;$y=~s/\///g<=>$x=~s/\///g} @F' file

# 4  
Old 12-23-2011
Thx,but i want shell.
# 5  
Old 12-23-2011
Code:
 awk -F/ '{print NF,$0}' myFile | sort -k1rn,1 | cut -d' ' -f2-

# 6  
Old 12-23-2011
No AWK:
Code:
while read l; do echo `echo $l | grep -o "/" | wc -l` $l; done <file | sort -rnk1 | cut -d" " -f2-

# 7  
Old 12-23-2011
Mostly Shell: Count the number of "/" characters, prepend the number to the line, sort, remove the prepended field.
Code:
#!/bin/ksh
cat filename.txt | while read line
do
        count=`echo "${line}"|tr -cd '/'|wc -c`
        echo "${count} ${line}"
done | sort -nr | cut -d' ' -f2-

This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate fixed length txt file

hi, i am using below query to generate the fixed length txt file. this sql is being called from shell script. This is supposed to be a fixed record file with the below definitions. There must be 2 byte filler after the CAT_ID AND each line should have total of 270 bytes. field ... (1 Reply)
Discussion started by: itzkashi
1 Replies

2. Shell Programming and Scripting

How to change variable length field?

Hello, I have a file with a date field with various lengths. For example: m/d/yyyy hh:mm or h:mm mm/dd/yyyy hh:mm or h:mm Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format. Thanks in... (6 Replies)
Discussion started by: sonnyo916
6 Replies

3. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

4. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

5. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

6. Shell Programming and Scripting

Delete lines where line length is < x

Hi all, I am using Sed to convert a log file into a csv, so far so good. Kudos to this forum for helping me thus far! My current problem. There are some lines in the log file that I do not want. How can I delete lines where the line legth is less than say 100? Here are some sample lines... (6 Replies)
Discussion started by: Mr. Rocco
6 Replies

7. Shell Programming and Scripting

Uniform length for all the lines in file

Hi, I have a file with different width for each line. like first line with 45characters and second line of 30 characters. But I want to make all the lines to 45 characters in file. Appreciate your inputs Thanks Arun: (1 Reply)
Discussion started by: arund_01
1 Replies

8. Shell Programming and Scripting

Pivot variable record length file and change delimiter

Hi experts. I got a file (500mb max) and need to pivot it (loading into ORCL) and change BLANK delimiter to PIPE |. Sometimes there are multipel BLANKS (as a particular value may be BLANK, or simply two BLANKS instead of one BLANK). thanks for your input! Cheers, Layout... (3 Replies)
Discussion started by: thomasr
3 Replies

9. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies
Login or Register to Ask a Question