Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Tags
sed, unix shell bash scripting

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 6 Days Ago   -   Original Discussion by boncuk
boncuk's Unix or Linux Image
boncuk boncuk is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 16 May 2018, 3:10 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #2  
Old Unix and Linux 6 Days Ago   -   Original Discussion by boncuk
rbatte1's Unix or Linux Image
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 22 May 2018, 10:49 AM EDT
Location: Lancashire, UK
Posts: 3,535
Thanks: 1,557
Thanked 692 Times in 622 Posts
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; 6 Days Ago at 12:35 PM.. Reason: Added grep option
Sponsored Links
    #3  
Old Unix and Linux 6 Days Ago   -   Original Discussion by boncuk
boncuk's Unix or Linux Image
boncuk boncuk is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 16 May 2018, 3:10 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
no it didnt help .. if i try to use "!" i get unknown command Linux

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 Unix and Linux 6 Days Ago   -   Original Discussion by boncuk
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 22 May 2018, 11:01 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts


Code:
sed -n '/^.\{21\}$/!p' a.a

Sponsored Links
    #5  
Old Unix and Linux 6 Days Ago   -   Original Discussion by boncuk
boncuk's Unix or Linux Image
boncuk boncuk is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 16 May 2018, 3:10 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rdrtx1 View Post


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 ?
Sponsored Links
    #6  
Old Unix and Linux 6 Days Ago   -   Original Discussion by boncuk
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 23 May 2018, 3:02 AM EDT
Location: Simplicity
Posts: 4,080
Thanks: 351
Thanked 1,377 Times in 1,241 Posts
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; 6 Days Ago at 03:19 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh sed - Extract specific lines with mulitple occurance of interesting lines popeye Shell Programming and Scripting 2 02-24-2014 03:57 PM
Finding size of files with spaces in their file names INNSAV1 Shell Programming and Scripting 4 08-07-2013 01:31 PM
Summing over specific lines and replacing the lines with the sum using sed, awk kaaliakahn Shell Programming and Scripting 3 06-25-2012 05:05 PM
help with finding specific files linuxlaptop UNIX for Dummies Questions & Answers 5 10-21-2008 02:30 PM
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



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