Any tricks on excluding a set of strings from a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Any tricks on excluding a set of strings from a file?
# 1  
Old 05-21-2013
Any tricks on excluding a set of strings from a file?

Hi,

Test file below:

Code:
$: cat file1
DATE          TIME    COL1    COL2  COL3  COL4   ID
01/10/2013    0800    100     200   300   401    112
01/31/2013    1000    201     123   345   456    107
03/05/2013    1100    150     789   311   789    109
02/15/2013    1500    199     456   234   555    105

If I want to exclude a set of strings, for example, I want a subset of the current file but without the following line of text? How do I do that?

01/31/2013 1000 201 123 345 456 107
02/15/2013 1500 199 456 234 555 105

At the moment, I am doing as below:

Code:
 
awk '{ print $0 }' file1 | grep -v "01/31/2013    1000    201     123   345   456    107" | grep -v "02/15/2013    1500    199     456   234   555    105"

Assuming that the ID column is what I am most interested in to exclude, I am also trying out

Code:
 
awk '$7 != 107 && $7 != 105' file1

But in case that assumption is wrong and I need to exclude some particular strings, is there any other way? I am thinking of creating a function/sub for it inside the script that I can call during the script when I needed to.

Any advice much appreciated. Thanks
# 2  
Old 05-21-2013
its better you store the lines in a file and use
Code:
 
grep -v -f exclude.txt filename.txt

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 05-22-2013
Quote:
Originally Posted by vidyadhar85
its better you store the lines in a file and use
Code:
 
grep -v -f exclude.txt filename.txt


Hi,

-f works on most of my servers but on some it doesn't like it 'coz I do not have the /usr/xpg4/bin/grep Smilie
# 4  
Old 05-22-2013
Use fgrep!
With awk you can exclude field 7 only:
Code:
awk 'FILENAME=="-" {a[$NF]=1; next} !($7 in a)' - file.txt < exclude.txt

The exclude-file only contains the IDs. awk reads it to a hashed array, then processes file.txt: prints if field 7 is not in the array.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

3. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

4. Shell Programming and Scripting

Getting lines between two strings with duplicate set of data

if I have the following lines in a file app.log some lines here <AAAA> abc <id>123456789</id> ddd </AAAA>some lines here too <BBBB> abc <id>123456789</id> ddd </BBBB>some lines here too <AAAA> xyz <id>987654321</id> ssss </AAAA>some lines here again... How do I get the... (5 Replies)
Discussion started by: nariwithu
5 Replies

5. Shell Programming and Scripting

PERL - Compare 2 strings, excluding whitespaces

I am creating a script to compare definitions and declarations of functions in C code, and report if the arguments are different. So I need to compare the 2 strings (args of both), how can I do that? (2 Replies)
Discussion started by: bojomojo
2 Replies

6. Shell Programming and Scripting

check file for presence of set of strings

hi everybody, I need a quick help with this issue. I have configuration file in which the following set of strings must be present. I need to check it with the bash script (leading spaces are not significant). Check must be case insensitive. Can anybody help? ... any lines <SECTION... (4 Replies)
Discussion started by: sameucho
4 Replies

7. Shell Programming and Scripting

excluding two or more groups of strings from printing

sample text: 001 the quick brown fox jumps 987 over a lazy dog 002 the quick brown fox jumps 999 over a lazy dog 003 the quick brown cow jumps 888 over a lazy dog 004 the quick brown fox jumps 777 over a lazy dog 005 the quick brown fox jumps 666 over a lazy cat i want to do something... (1 Reply)
Discussion started by: marcpascual
1 Replies

8. UNIX for Dummies Questions & Answers

Set prompt, problems and tricks

I'm using a csh shell (or, that'd be my guess from the .cshrc file I see) and I'm looking to change my prompt. There are about 10 other threads, I know, but this question is a little more specific. I want to know, is there a way to list the current directory from a certain level or directory... (6 Replies)
Discussion started by: HybridLogic
6 Replies

9. Shell Programming and Scripting

extracting a set of strings from a text file

i have textfiles that contain a series of lines that look like this: string0 .................................................... column3a column4a string1**384y0439 ..................................... column3b column4b... (2 Replies)
Discussion started by: Deanne
2 Replies
Login or Register to Ask a Question