Searching a file with exclusion in the search


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Searching a file with exclusion in the search
# 1  
Old 11-27-2008
Searching a file with exclusion in the search

Hello,
I'm looking for a bit of help. Im trying to search a file for lines that contain white spaces at the end of the lines. This is what I'm using where $param is the path and file name and it redirects the output to a txt file :

echo | grep -n ' $' $param >> $2

Is it possible to have exclusions within my expression?
I want to search for white spaces but not ones that are tabs(\t) and ones that start with (#) or('^#) or ones that just have a space a the start of a line ('^ $').

Maybe its possible to filter it through by using pipe but im not sure how to do this.

Any help would be great. Thanks.
# 2  
Old 11-27-2008
Try this with sed:

Code:
sed -n '/^[a-zA-Z1-9].* $/p' "$param " > "$2"

Regards
# 3  
Old 11-27-2008
Thanks. That works good but that won't exclude lines that have a tab character at the end or lines. Also I mean to mention that id like to exclude lines that start with the whitspace character.

Sorry if im not explaing myself clearly.

Last edited by gintreach; 11-27-2008 at 12:48 PM.. Reason: Tested it some more
# 4  
Old 11-27-2008
It works for me, this is an example of a file. All the lines except the line that ends with a tab contain a space as last character. The output is only the line with a space at the end of the line.

Code:
$ cat file
 start with a space 
    start with a tab 
space as last character 
tab as last character    
#start with # 
^#start with ^# 
$
$
$ sed -n '/^[a-zA-Z1-9].* $/p' file
space as last character
$

# 5  
Old 11-28-2008
Ya, sorry Franklin, it does work. It was just another problem in my script giving me trouble.

My final command im using is this
sed -n '/^[a-zA-Z1-9].*[ \t]$/p' $param >> $2
You can probably see im also looking for line that finish in a tab also and redirecting my output to another file.

Just have one final question. Can I also output the line number of my results?
I'm trying to use the = command but getting results with the line number appear above the line rather than along side it. Do you know is there a simpler way?

Thanks so far for your help.
# 6  
Old 11-28-2008
Use awk instead of sed:

Code:
awk '/^[a-zA-Z1-9].*[ \t]$/{print ++i, $0}' file

Regards
# 7  
Old 12-01-2008
Hi again.
That outputs the line number of the resulting file, but is it possible to output the line number of the file that the result was found in??

Don't think I was too clear in my question, sorry.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

2. Shell Programming and Scripting

Pattern exclusion between two files

I have a large file (file1) that has 96770154 lines and a smaller file (file2) that has 3 lines. I want to remove all of the strings from file1 that occur in file2. file1 looks like this: DOGDOGNODOGTESTCAT CATHELLOBYEEBYEFAT CATCATDOGDOGCATYESGOOD file2 looks like this: YES... (10 Replies)
Discussion started by: verse123
10 Replies

3. Shell Programming and Scripting

Mutual exclusion in getopts

Can i use mutual exclusion of particular args and force script to exit if both are specified? while getopts l:d: OPTS; do case $OPTS in l) VALUE1=$OPTARG;; d) VALUE2=$OPTARG;; *) echo "$USAGE" && exit 2;; # Here i want to exit if both -l and -d are specified ... (1 Reply)
Discussion started by: urello
1 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

5. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

6. Shell Programming and Scripting

Using variables for exclusion in tar

Hello. I am writing a backup script which uses a function to create a tar archive for a specified directory. For some reason though I cannot get tar to exclude a specified path when passed as a variable. The problem does not seem to be with the command though as it works when entered in the... (2 Replies)
Discussion started by: mij
2 Replies

7. Shell Programming and Scripting

Delete old files but with exclusion with file list

Hello Can you please help and check what im missing on script below the goal is to delete the old files more than 7 days old but not the excluded file list inside excluded.dat file #!/bin/sh EXCLUDE=/path/to/exclude/exclude.dat FIND=/bin/find for xfile in '(read $EXCLUDE)' do $FIND... (9 Replies)
Discussion started by: angst_nu
9 Replies

8. Solaris

Search for string in a directory with binaries exclusion

Hi all, My need is to search for a given string "toto" within a directory, but the search should ignore binary and db files. It is in fact, a combination of xargs grep that I didn't succeed to manage. Let we say that we are under /etc/ and we want to search for all included files (except... (9 Replies)
Discussion started by: mdjebbi
9 Replies

9. Shell Programming and Scripting

Exclusion List of file and directory

dear all i trying to list all files within a directory. I want to exclude all subdirectory and some files, with using below statement, but it not exclude the files which start with "&" and end with "SL" , is there any things wrong with the below statement ? TIA cd /myaccount/mydirectory... (6 Replies)
Discussion started by: ayang
6 Replies

10. Shell Programming and Scripting

searching for filenames with search strings in another file

Hi, I have 5 files in a directory. emp1_usage.txt emp2_usage.txt emp3_usage.txt emp4_usage.txt emp5_usage.txt I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table. for f in *emp*.txt do sqlldr... (3 Replies)
Discussion started by: pathanjalireddy
3 Replies
Login or Register to Ask a Question

Featured Tech Videos