Filter ONLY lines with non-printing charaters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter ONLY lines with non-printing charaters
# 1  
Old 12-07-2016
Filter ONLY lines with non-printing charaters

I have a file contains data with non-printing characters. i have used
Code:
cat -v filename

to display whole data with non-printing characters also.
However, i need lines with non-printing characters into seperate file. My file is huge and looks like i have to manully find lines using
Code:
cat -v filename | more

any help how to implement a script...
# 2  
Old 12-07-2016
How about
Code:
grep '[^[:print:]]' filename

?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-07-2016
You haven't really described what you consider non-printing characters. In most locales, RudiC's suggestion will select any lines that contain any character that is not <space> and is not in class alpha, not in class digit, and not in class punct (not counting the line terminating <newline> character). For many text files, you might also want to keep lines containing <tab> characters. If that is true in your case, you might try this slight modification to RudiC's suggestion:
Code:
grep '[^[:print:][:blank:]]' filename

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 12-07-2016
To add a different take: if you mean by "non-printable" characters those which cannot be displayed because of your locale (like, for instance, UTF-8-characters with a "C"-locale) you can use seds l-command (display characters in a visually unambiguous form) to display these.

For instance, the following file content (german umlauts):

Code:
xx Ä xx
xx ö xx

would be displayed as:

Code:
# sed -n 'l' umlaut.file
xx \303\204 xx$
xx \303\266 xx$

Note, that this is NOT a translation, so you cannot do further work on the pattern space and transform the resulting 3-digit (octal) codes. Save the resulting file instead and then start a new sed (grep, ...) -command to further process it, i.e. to select all the lines with umlauts:

Code:
# sed -n 'l' umlaut.file > result
# grep '\\[0-7][0-7][0-7]' result

I hope this helps.

bakunin

Last edited by bakunin; 12-07-2016 at 07:40 PM..
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System V printing filter

First, please excuse my apparent lack of attempt as this is NOT the case. I have attempted to research this for hours and realize I am way out of my league. I am not a programmer, especially in Unix. I have an old Alpha Unix system with a program that prints to a network printer using the LPR... (4 Replies)
Discussion started by: Ken_Snauffer
4 Replies

2. Shell Programming and Scripting

awk to filter multiple lines

Hi. I need to filter lines based upon matches in multiple tab-separated columns. For all matching occurrences in column 1, check the corresponding column 4. IF all column 4 entries are identical, discard all lines. If even one entry in column 4 is different, then keep all lines. How can I... (5 Replies)
Discussion started by: owwow14
5 Replies

3. UNIX for Dummies Questions & Answers

Filter lines common in two files

Thanks everyone. I got that problem solved. I require one more help here. (Yes, UNIX definitely seems to be fun and useful, and I WILL eventually learn it for myself. But I am now on a different project and don't really have time to go through all the basics. So, I will really appreciate some... (6 Replies)
Discussion started by: latsyrc
6 Replies

4. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

5. Shell Programming and Scripting

How to filter only the last 'n' lines of a grep output?

I am running a grep query for searching a pattern, and the output is quite huge. I want only the last 200 lines to be displayed, and I am not sure if tail will do the trick (can tail read from std in/out instead of files?). Please help me out. (1 Reply)
Discussion started by: shell_newbie
1 Replies

6. Shell Programming and Scripting

filter out a sequence from multiple lines line

Hi, I have an unwanted string at random lines of my verilog (*.v) file. (* abccddee *) input A; (* xyz *) input B; (* 1234 *) output C; I want a clean file like this: input A; input B; output C; the unwanted string begins with "(*" and ends with "*)" at multiple lines. Any help... (2 Replies)
Discussion started by: return_user
2 Replies

7. Shell Programming and Scripting

printing positional charaters in a loop

#!/bin/bash usep=`df -hT | awk '{ print $5 }'` for (1=1,1<8,i++) output=`echo $usep | awk '{ print $i }'| cut -d'%' -f1` echo $output if then echo "critical value" i need to echo critical value if disk usage pecentage xceeds 10 and i am face problem in position marked red here i... (9 Replies)
Discussion started by: josgeorge
9 Replies

8. UNIX for Advanced & Expert Users

unable to filter out blank lines ^$ matches nothing

Hi I've got a file with a lot of blank lines in it or are they?...I tried to remove them by using grep -v ^$ <filename> This did not remove anything....I then tried the reverse to see if ^$ indeed matches anything...so I tried $ grep ^$ grepres.txt $ grep ^\s*$ grepres.txt This... (5 Replies)
Discussion started by: venkatkk
5 Replies

9. Shell Programming and Scripting

Filter unwanted lines

Hi All, I have the below input and i only want to filter out some un-wanted info from here. Expected output is below. Can somebody help ? The catch is that i want to grep those lines with term "k=" and lines with term "**" as the 1st column and "07" as the last column. And the number of... (15 Replies)
Discussion started by: Raynon
15 Replies

10. Shell Programming and Scripting

how do I filter double lines from a txt file

Hi, I have a file with the following: access-list.txt router1 access-list 1 permit any any access-list 1 deny any any router2 access-list 2 permit any any access-list 2 deny any any router3 access-list 3 permit any any access-list 3 deny any any I want to hava an output that... (10 Replies)
Discussion started by: I-1
10 Replies
Login or Register to Ask a Question