How to delete all lines with less then 32 characters from a textfile?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete all lines with less then 32 characters from a textfile?
# 8  
Old 02-25-2010
@anbu23 and kcoder24:

heheh. nicely done.

Quote:
Originally Posted by kcoder24
My AWK version

Code:
'/.{32,}/'

The AWK implementation on the machine in front of me doesn't support brace intervals, but I assume you can shorten that just a bit more to:
Code:
'/.{32}/'

Smilie

Cheers.

Last edited by alister; 02-25-2010 at 03:09 PM..
# 9  
Old 02-25-2010
We can also remove the lines which are having less than 32 characters in a file by using the following command.

sed -ir '/^.{0,31}$/d' <inputfilename>
# 10  
Old 02-25-2010
Try:

Code:
sed "/^.\{0,31\}$/d" file

EDIT: Just noticed that the above poster beat me to that answer. Oh, well, it was a good exercise.

Last edited by Ultrix; 02-26-2010 at 12:15 AM..
# 11  
Old 02-26-2010
Code:
use strict; 
use warnings;  
open FH,"file"; #this is your input file
open RFH, "> output"; #this will be the output file which will have the lines from the input file longer than 32 characters 

while ( <FH> )  #reading the data from the file
{     
           if ( length $_ > 32 ) #if the line length is greater than 32    
           {         
                   print RFH $_; #writing to the output file     
           } 
}

This is a Perl script.
The file will contain the input data.
After executing the above code the output file will contain the lines which are all having length greater than 32 characters.

Last edited by thillai_selvan; 03-09-2010 at 01:07 AM..
# 12  
Old 02-26-2010
BTW, what is the practical value of what you're trying to do? I really want to know, because I just wrote a shell script that does what you described, only way better, and I'm wondering how I can use it.

---------- Post updated 02-26-10 at 12:57 AM ---------- Previous update was 02-25-10 at 11:54 PM ----------

Quote:
Originally Posted by thillai_selvan
Code:
 use strict; 
use warnings;  
 open FH,"file"; #this is your input script 
my @array = ; #reading the file content in the input file 
open RFH,"> output"; #this will be the output file which will have the lines from the input file longer than 32 characters foreach ( @array ) 
{     
           if ( length $_ > 32 ) #if the line length is greater than 32    
           {         
                   print RFH $_; #writing to the output file     
           } 
}

This is a Perl script.
The file will contain the input data.
After executing the above code the output file will contain the lines which are all having length greater than 32 characters.
Why use an 11 line Perl script when you can use a 1 line shell script? I tested my method and it works perfectly. I also modified it so that it takes an arbitrary range of line lengths, as well as allowing the user to decide whether to write to the file:
Code:
#!/bin/bash
# Deletes lines of a certain range length from a file.
# Writing the result back to the file is optional.

write="no";
for opt in $*
do
    case $opt in
        -w ) write="yes";
             shift;;
        -* ) shift;;
        *  );;
    esac
done
if [ $write == "yes" ]
then
    sed -e "/^.\{$1,$2\}$/d" $3 > tempfile.txt;
    cat tempfile.txt > $3;
    rm tempfile.txt;
else
    sed -e "/^.\{$1,$2\}$/d" $3;
fi

# 13  
Old 02-26-2010
I answered this for the actual poster. Not indented to point to your script.
Perl is also an efficient script. Thats why I wanted to guide the questioner to get fame on my views
# 14  
Old 02-26-2010
Quote:
Originally Posted by Ultrix
I tested my method and it works perfectly. I also modified it so that it takes an arbitrary range of line lengths, as well as allowing the user to decide whether to write to the file:
Code:
#!/bin/bash
# Deletes lines of a certain range length from a file.
# Writing the result back to the file is optional.

write="no";
for opt in $*
do
    case $opt in
        -w ) write="yes";
             shift;;
        -* ) shift;;
        *  );;
    esac
done
if [ $write == "yes" ]
then
    sed -e "/^.\{$1,$2\}$/d" $3 > tempfile.txt;
    cat tempfile.txt > $3;
    rm tempfile.txt;
else
    sed -e "/^.\{$1,$2\}$/d" $3;
fi

Did you test it with filenames containing IFS characters? Assuming a default IFS value, your option handling, sed invocations, and cat statement will all barf if a filename contains whitespace.

Code:
for opt in $*

should be
Code:
for opt in "$@"

All instances of $3 need to be double-quoted.

Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replicating certain lines in a textfile

I am very new to to shell scripting and facing a problem that I can't seem to solve. I want to write a bash script that edits file1.txt and saves it as file2.txt. This is what the files should look like: file1: textline1 textline2 startCopy copyThis endCopy textline3 textline4 file2: ... (6 Replies)
Discussion started by: sandy90
6 Replies

2. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

Cut lines from and to in a textfile

i am having a text file like below rama surya pandu latha singh raja i want to get the new file from 3 to 5 i.e pandu latha singh please help (1 Reply)
Discussion started by: suryanarayana
1 Replies

4. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

7. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

8. UNIX for Dummies Questions & Answers

How get only required lines & delete the rest of the lines in file

Hiiii I have a file which contains huge data as a.dat: PDE 1990 1 9 18 51 28.90 24.7500 95.2800 118.0 6.1 0.0 BURMA event name: 010990D time shift: 7.3000 half duration: 5.0000 latitude: 24.4200 longitude: 94.9500 depth: 129.6000 Mrr: ... (7 Replies)
Discussion started by: reva
7 Replies

9. Shell Programming and Scripting

Detect lines beginning with double-byte characters (Japanese) and delete

Greetings, I want to use a script (preferably awk) which determines if the first character in a line is double-byte (as in Japanese or Chinese) and deletes it. For example: (in the above quote, I see Japanese on my screen for two lines - with 2 characters in the first and 3 characters in the... (8 Replies)
Discussion started by: ubbeauty
8 Replies

10. Shell Programming and Scripting

How to delete lines in a file that have duplicates or derive the lines that aper once

Input: a b b c d d I need: a c I know how to get this (the lines that have duplicates) : b d sort file | uniq -d But i need opossite of this. I have searched the forum and other places as well, but have found solution for everything except this variant of the problem. (3 Replies)
Discussion started by: necroman08
3 Replies
Login or Register to Ask a Question