Selecting lines with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting lines with sed
# 8  
Old 05-20-2010
Quote:
Originally Posted by allinshell
Code:
sed -e '/^[-.0-9A-Za-z\s]$/p' file1

Can anybody help on this.
\s is not valid in posix-compliant posix (perhaps it's a gnu sed extension, but I'm not sure). Even if it's allowed, that regular expression would only match a line with one matching character. And, since sed prints all lines by default, the p command in this case will cause matching lines to print twice.

Two portable sed alternatives:
Code:
sed -n '/^[[:alnum:][:blank:].-]*$/p' file1
sed '/^[[:alnum:][:blank:].-]*$/!d' file1

The first disabled printing by default, with the -n option, and then only prints lines that consist of nothing but the allowed characters. The second option deletes all lines that do not consist of all matching characters.

Regards,
Alister

---------- Post updated at 11:56 AM ---------- Previous update was at 11:53 AM ----------

I've been having vision troubles the past few days. Seconds after posting, I noticed anbu's post. Mine is essentially the same, except it uses character classes.
# 9  
Old 05-24-2010
Thanks a lot anbu23 and Alister, it was very helpful. I have a doubt. how can i do this task for a particular part of the line..for example, i want to check for the special characters in all lines from position 10 to 20 only..
# 10  
Old 05-24-2010
Code:
$ cat file
691775025 ýÄqJ8^Z^Y{ 2004-08-23E P 100.00
45585025 0527541139295037342008-07-25OEP 100.00
6983025 ýB<9D>x<^F^Xb 2004-11-16SPP 100.00
$ awk ' !gsub("[^-.0-9A-Za-z ]","",substr($0,10,10)) ' file
45585025 0527541139295037342008-07-25OEP 100.00

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting lines having same values for first two columns

Hello to all. This is first post. Kindly excuse me if I do not adhere to any rules and regulations of this forum. I have a file containing some rows with three columns each per row(separeted by a space). There are certain rows for which first two columns have same value but the value in... (6 Replies)
Discussion started by: manojmalhotra13
6 Replies

2. Shell Programming and Scripting

sum of a column and selecting lines with value above threshold

Hi again, I need to further process the results of a previous manipulation. I have a file with three columns e.g. AAA5 0.00175 1.97996e-06 AAA5 0.01334 2.14159e-05 AAA5 0.01340 4.12155e-05 AAA5 0.01496 1.10312e-05 AAA5 0.51401 0.0175308 BB0 0.00204 2.8825e-07 BB0 0.01569 7.94746e-07 BB0... (6 Replies)
Discussion started by: f_o_555
6 Replies

3. Shell Programming and Scripting

selecting and deleting specific lines with condition

I have a set of data as below: The first field, $1 represent "|". The $3 (3rd field) and $6 (6th field) in my data file represent "number-molecule" which has arrangement as below: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... (9 Replies)
Discussion started by: vjramana
9 Replies

4. Shell Programming and Scripting

selecting lines using awk

I have a file which contains five hundred thousand lines (500,000). I want to select lines of every 1000 lines from that file using AWK. I can think something like this in bash as below: for lines in {0..500000..1000} do ........ ........ done But I want to use AWK. I do not know how to... (4 Replies)
Discussion started by: vjramana
4 Replies

5. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

6. Shell Programming and Scripting

Selecting lines of a file

Say I wanted to select the 5th line of a file without knowing the context of the file. Would I use grep and pipe it into wc or is there a more simple way of doing this? (3 Replies)
Discussion started by: puttster
3 Replies

7. Shell Programming and Scripting

Selecting a range of Lines

Hi All, Is there a way to get a range of lines from a file??? I want to search through a set of scripts and need to select the group of lines which do the FTP. Say, Line1 Line2 ftp SERVER user UNAME PASS send FILE_TO_BE_SENT close Line3 Line4 Line5 ftp SERVER1 user USER1 PASS1... (6 Replies)
Discussion started by: beinthemiddle
6 Replies

8. Shell Programming and Scripting

about selecting lines

Hello , i got text file like that' C:\Users\Public\Pictures\Sample Pictures\aa.jpg C:\Users\Public\Pictures\Sample Pictures\thumb.jpg C:\Users\Public\Pictures\vv\cc.jpg C:\Users\Public\Pictures\Sample Pictures\ee.jpg C:\Users\Public\aa\Sample Pictures\cvswsr.jpg... (1 Reply)
Discussion started by: davidkhan
1 Replies

9. Shell Programming and Scripting

Selecting Lines on text file

Hi All, I am creating a script that sends log data from text files to a Database and I will like to read sugestions, as I think that there might be better ways to achive this than with my shell script; maybe perl or I don't know, but I will like to read some sugestions. The log is from... (10 Replies)
Discussion started by: oconmx
10 Replies

10. Shell Programming and Scripting

selecting only few lines from many based on a common pattern

Hi, I have a file having some thousand records with the following sort of lines: Error: Failed to get order data Order: PO-BBBTGZE Error: No CLI Error: Failed to get order data Order: PO-SBDJUZA Order: PO-XBBIDEN Error: No CLI Error: Failed to get order data Order: PO-BBDJUTQ Order:... (2 Replies)
Discussion started by: damansingh
2 Replies
Login or Register to Ask a Question