Finding lines of specific size in files using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding lines of specific size in files using sed
# 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 01:35 PM.. Reason: Added grep option
# 3  
Old 05-16-2018
no it didnt help .. if i try to use "!" i get unknown command Smilie

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 04:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Dummies Questions & Answers

Finding Files only under a specific FileSystem

Hi, I am using AIX and one of my file systems is getting filled up and I need to track with files are occupying more volume. Filesystem GB blocks Free %Used Iused %Iused Mounted on /dev/nhdb_lv 2110.00 63.80 97% 76525 1% /nhdb under the Mount Point /nhdb... (1 Reply)
Discussion started by: zulfi123786
1 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Finding size of files with spaces in their file names

I am running a UNIX script to get unused files and their sizes from the server. The issue is arising due to the spaces present in the filename/folder names.Due to this the du -k command doesn't work properly.But I need to calculate the size of all files including the ones which have spaces in them.... (4 Replies)
Discussion started by: INNSAV1
4 Replies

5. Shell Programming and Scripting

Finding 4 current files having specific File Name pattern

Hi All, I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length. 1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number... (6 Replies)
Discussion started by: lancesunny
6 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. UNIX for Dummies Questions & Answers

help with finding specific files

before i get to it, i would like to say this is the greatest unix site ive ever seen, and im glad to see so many people are out there to help. thanks well, im trying to make myself a script where i can specify a directory and a file size so that my script will show me any files larger than the... (5 Replies)
Discussion started by: linuxlaptop
5 Replies

8. Shell Programming and Scripting

Finding cumulative size of files older than certain days

Hi All, I've got a ton of files in a particular directory. I want to find pdf files older than 30 days in that directory and then the cumulative size of those files. Ex: find /home/jk/a -name "*.pdf" -mtime +30 consider it finds the below 4 files. /home/jk/a/1.pdf /home/jk/a/2.pdf... (1 Reply)
Discussion started by: rohan076
1 Replies

9. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

10. Shell Programming and Scripting

Finding a specific pattern from thousands of files ????

Hi All, I want to find a specific pattern from approximately 400000 files on solaris platform. Its very heavy for me to grep that pattern to each file individually. Can anybody suggest me some way to search for specific pattern (alpha numeric) from these forty thousand files. Please note that... (6 Replies)
Discussion started by: aarora_98
6 Replies
Login or Register to Ask a Question