Delete lines where line length is < x


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines where line length is < x
# 1  
Old 07-13-2010
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 from my log file:
so in this example, it would be safe to say that I want to delete any line that is less than 100 characters long

Thanks in advance to the community (I hope I can contribute someday! Smilie)
# 2  
Old 07-13-2010
sed is not keen at doing arithmetic evaluation. Smilie
You should need first to evaluate length though an *sh script, then pass line to sed, but then you loose sed's benefit.
using awk from the beginning, may be more obvious
# 3  
Old 07-14-2010
Quote:
Originally Posted by daPeach
sed is not keen at doing arithmetic evaluation. Smilie
You should need first to evaluate length though an *sh script, then pass line to sed, but then you loose sed's benefit.
using awk from the beginning, may be more obvious
Nah. It can be easily done with sed. The following will delete all lines that are less than 100 characters long:
Code:
sed '/.\{100\}/!d'

Regards,
Alister
# 4  
Old 07-14-2010
Here you go.
Code:
#!/bin/ksh
set -x
logfile=$dir/logfilename.txt
thr=100
cat $logfile | while read a
do
nochar = `echo $a | wc -c`
sleep 1
if (( $nochar < $thr )) then
echo echo "$a" >>new.logfile.log
else "no. of chars less than 10";
fi
done

Let me know if this helps.

Quote:
Originally Posted by Mr. Rocco
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 from my log file:
so in this example, it would be safe to say that I want to delete any line that is less than 100 characters long

Thanks in advance to the community (I hope I can contribute someday! Smilie)
Thanks,
Sameer.

Last edited by ensameer; 07-14-2010 at 01:20 AM.. Reason: typo error updated thr=100
# 5  
Old 07-14-2010
Thanks so much all, will test them out and see which will be the most appropriate for my scenario. =]
# 6  
Old 07-14-2010
by awk:

Code:
awk 'length>100 ' urfile

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 07-14-2010
Quote:
Originally Posted by ensameer
Here you go.
Code:
#!/bin/ksh
set -x
logfile=$dir/logfilename.txt
thr=100
cat $logfile | while read a
do
nochar = `echo $a | wc -c`
sleep 1
if (( $nochar < $thr )) then
echo echo "$a" >>new.logfile.log
else "no. of chars less than 10";
fi
done

That read command will discard leading and trailing whitespace and mishandle any occurrences of a backslash, leading to erroneous line length counts. Also, by using ${#a}, you can determine length without using wc.

Code:
while IFS= read -r a; do
    [ ${#a} -ge 100 ] && echo "$a"
done < "$logfile" >> "$logfile".new

Regards,
Alister

Last edited by alister; 07-14-2010 at 06:45 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines except a line starting with string

Shell : bash OS : RHEL 6.8 I have a file like below. $ cat pattern.txt hello txt1 txt2 txt3 some other text txt4 I want to remove all lines in this file except the ones starting with txt . How can I do this ? (4 Replies)
Discussion started by: omega3
4 Replies

2. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

3. Shell Programming and Scripting

Need to delete all lines where any line meets a condition

Here is an example of a file... foo1,good foo1,good foo2,error foo2,good Note that both rows for foo1 have good in the 2nd field, but one of the foo2 rows has error... I need something in ksh/awk/perl that will delete ALL foo2 lines if ANY of them have error in the 2nd field...so: ... (7 Replies)
Discussion started by: dbiggied
7 Replies

4. Shell Programming and Scripting

How to delete several lines from file by line number?

Hi I am using the following command to delete a line from the file by line number: line_number=14 sed "${line_number}d" inputfilename > newfilename Is there a way to modify this command to specify the range of lines to be deleted, lets say from line 14 till line 5 ? I tried using the... (5 Replies)
Discussion started by: aoussenko
5 Replies

5. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

6. Shell Programming and Scripting

delete multiple lines by line number

I have file with 10000 records and i need to delete the lines in single shot based on line number range say from 10 to 51 , 53 to 59 , 105 to 107, 311 to 592 etc... between range works fine for me but how to achive for above case? please help sed '10,51 {d}' infile > outfile (5 Replies)
Discussion started by: zooby
5 Replies

7. Shell Programming and Scripting

Delete all lines after a specific line ?

Hello. My file is like this: a b c d e f g h i I want to delete all lines after the 3rd line, means after the "c". Is there any way to do this? The lines differ between them and the lines I want to delete does not have a specific word, or the lines I want to keep (a,b,c) does not have a... (4 Replies)
Discussion started by: hakermania
4 Replies

8. Shell Programming and Scripting

Delete lines with line numbers.

Hi, I have a file will 1000 lines.... I want to deleted some line in the file... like 800-850 lines i want to remove in that... can somebody help me..? thanks. (2 Replies)
Discussion started by: Kattoor
2 Replies

9. Shell Programming and Scripting

search 2 lines and delete above line

Hi, I've been searching in this forum for the last 4 hours trying to do one thing: search 2 lines and delete the above line. So far I have not be able to find something similar in this forum, so I need help. This is what I'm trying to do. For example, I have a file called file1: file1 word1... (4 Replies)
Discussion started by: shamushamu
4 Replies

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