Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Finding lines of specific size in files using sed

Tags
sed, shell scripts, unix shell bash scripting

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-16-2018
Finding lines of specific size in files using sed

i am using sed to detect any lines that are not exactly 21. the following gives me the lines that ARE exactly 21. i want the opposite , i want the two lines that are not size 21 (shown in bold)

Code:
type a.a
000008050110010201NNN
000008060810010201NNN
21212000008070110010201NNN
000008080310010201NNN
000008090510010201NNN
000008050110010201NNN
000008060310010201NNN
000008070110010201NNN
SUMM0001583381


sed -n "/^.\{20,21\}$/p" a.a
000008050110010201NNN
000008060810010201NNN
000008080310010201NNN
000008090510010201NNN
000008050110010201NNN
000008060310010201NNN
000008070110010201NNN

# 2  
Old 05-16-2018
I think that this might give you lines that are 20 OR 21 characters long, but I'm not certain. A quick test should prove that to you. How does your code run for you with this as the input:-
Code:
12345678901234567890
123456789012345678901
1234567890123456789012
12345678901234567890123

Usually the ! will negate a search, so sed -n "!/Hello world/p" might print the unmatched lines. Does that help?

As an alternate, grep can use a regular expression, so grep -E '^.{21}$' a.a would match a 21 character line and grep -Ev '^.{21}$' a.a should match the others



Given the behaviour of type, can I assume that this is an emulation rather than a Unix/Linux server?


I hope that this helps, but please show the output/errors and we can dig in further if I've got it wrong.



Kind regards,
Robin

Last edited by rbatte1; 05-16-2018 at 12:35 PM.. Reason: Added grep option
# 3  
Old 05-16-2018
no it didnt help .. if i try to use "!" i get unknown command

sed -n '!/^.\{20,21\}$/p' a.a
sed: Unknown command

---------- Post updated at 12:54 PM ---------- Previous update was at 12:36 PM ----------

i just noticed that my original command is flawed too .. its failing in this particular scenario , notice its showing the line that is not 21 ?

I just need a sed command that will give me all lines that are not exactly 21 .

Code:
type a.a
000008050110010201NNN
000008060810010201NNN
21212000008070110010201NNN
000008080310010201NNN
000008090510010201NNN
000008050110010201NNN
000008060310010201NNN
00008070110010201NNN
SUMM0001583381

$ sed -n '/^.\{20,21\}$/p' a.a
000008050110010201NNN
000008060810010201NNN
000008080310010201NNN
000008090510010201NNN
000008050110010201NNN
000008060310010201NNN
00008070110010201NNN

# 4  
Old 05-16-2018
Code:
sed -n '/^.\{21\}$/!p' a.a

# 5  
Old 05-16-2018
Quote:
Originally Posted by rdrtx1
Code:
sed -n '/^.\{21\}$/!p' a.a

thanks

is it possible to break out of sed on the first non match instead of looping through the whole large file ?
# 6  
Old 05-16-2018
Yes simply narrow the range to \{21,21\} or shorter \{21\}
Alternative: delete the lines that are exactly 21 characters long
Code:
sed '/^.\{21,21\}$/d' a.a

The d command jumps to the next input cycle, so a following quit command is run only when nothing is deleted.
Code:
sed '/^.\{21,21\}$/d;q' a.a

This is much simpler than
Code:
sed -n '/^.\{21\}$/!{p;q;}' a.a


Last edited by MadeInGermany; 05-16-2018 at 03:19 PM..
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find files containing two specific lines and delate those lines? androwida Shell Programming and Scripting 4 10-12-2016 12:08 PM
Finding Files only under a specific FileSystem zulfi123786 UNIX for Dummies Questions & Answers 1 12-08-2014 07:21 AM
Finding size of files with spaces in their file names INNSAV1 Shell Programming and Scripting 4 08-07-2013 01:31 PM
Finding 4 current files having specific File Name pattern lancesunny Shell Programming and Scripting 6 11-02-2012 02:32 PM
help with finding specific files linuxlaptop UNIX for Dummies Questions & Answers 5 10-21-2008 02:30 PM
Finding cumulative size of files older than certain days rohan076 Shell Programming and Scripting 1 04-28-2008 01:07 PM
Unix shell script for finding top ten files of maximum size abhilashnair UNIX for Dummies Questions & Answers 10 02-18-2008 12:50 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 03:20 AM
Generating files of specific size nxd25 Shell Programming and Scripting 2 06-27-2006 11:06 AM
Finding a specific pattern from thousands of files ???? aarora_98 Shell Programming and Scripting 6 02-17-2006 07:28 AM


All times are GMT -4. The time now is 03:05 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password