Remove matching lines with list of strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove matching lines with list of strings
# 1  
Old 10-15-2008
Question Remove matching lines with list of strings

Hi,
HP-UX gxxxxxxxc B.11.23 U ia64 3717505098 unlimited-user license
I have a file with below pipe separated field values:

xxx|xxx|abcd|xxx|xxx|xx
xxx|xxx|abcd#123|xxx|xxx|xx
xxx|xxx|abcd#345|xxx|xxx|xx
xxx|xxx|pqrs|xxx|xxx|xx
xxx|xxx|pqrs#123|xxx|xxx|xx

The third field has values like abcd and pqrs. I need a file with lines only with abcd and pqrs. The other lines that have abcd#123, abcd#345 should be removed. Same for pqrs field also. This is a huge file and many in numbers. So, I need some expert suggestion to have an efficient solution.

I have used awk -F"|" '{print $3}' | grep -v "#". It only gives me abcd and pqrs. I need the corresponding lines in a separate file. My output file should be:

xxx|xxx|abcd|xxx|xxx|xx
xxx|xxx|pqrs|xxx|xxx|xx

I have a great respect to the knowledgeable participants and their willingness to help in this forum and also I have taken a lot of help in finding my answers in the past from you guys. I know I can't be let down. Please help, it is important for me to remain as a developer. Thanks in advance.

Manjax
# 2  
Old 10-15-2008
Hammer & Screwdriver Is this what you are trying to do?

Code:
> cat file98
xxx|xxx|abcd|xxx|xxx|xx
xxx|xxx|abcd#123|xxx|xxx|xx
xxx|xxx|abcd#345|xxx|xxx|xx
xxx|xxx|pqrs|xxx|xxx|xx
xxx|xxx|pqrs#123|xxx|xxx|xx

> awk -F"|" '$3=="abcd" || $3=="pqrs" {print}' file98
xxx|xxx|abcd|xxx|xxx|xx
xxx|xxx|pqrs|xxx|xxx|xx

> awk -F"|" '$3!="abcd" && $3!="pqrs" {print}' file98
xxx|xxx|abcd#123|xxx|xxx|xx
xxx|xxx|abcd#345|xxx|xxx|xx
xxx|xxx|pqrs#123|xxx|xxx|xx

or

Code:
> awk  'BEGIN {FS="|"} $3=="abcd" || $3=="pqrs" {print}' file98
xxx|xxx|abcd|xxx|xxx|xx
xxx|xxx|pqrs|xxx|xxx|xx

> awk  'BEGIN {FS="|"} $3!="abcd" && $3!="pqrs" {print}' file98
xxx|xxx|abcd#123|xxx|xxx|xx
xxx|xxx|abcd#345|xxx|xxx|xx
xxx|xxx|pqrs#123|xxx|xxx|xx


Last edited by joeyg; 10-15-2008 at 05:24 PM.. Reason: Added two more awk commands
# 3  
Old 10-15-2008
Question Remove matching lines with list of strings

Hi,

I should mention that the strings may be anything. abcd and pqrs were just examples. All I know is that there is a third field string with and without #. I need lines without #. Hope we are on the same page. Thanks for the quick come back.

Manjax
# 4  
Old 10-15-2008
Code:
grep '^[^|]*\|[^|]*\|[^#|]*\|' infile>outfile

If you grep implementation supports re-interval (like /usr/xpg4/bin/egrep on Solaris and GNU grep):

Code:
egrep '^([^|]*\|){2}[^#|]*\|' infile>outfile


Last edited by radoulov; 10-16-2008 at 04:59 AM.. Reason: corrected
# 5  
Old 10-29-2008
MySQL Thank you very much

Hi all,

Firstly, I am extremely sorry that I was late to reply and say thanks. Everytime I log out from this site happily. Thanks guys, keep up the good work. You people are wonderful.

Regards,
Manjax
# 6  
Old 10-29-2008
Code:
nawk -F"|" '($3 ~ /^abcd$/ || $3 ~/^pqrs$/){print}' filename

# 7  
Old 10-29-2008
RE:Remove matching lines with list of strings

code:

nawk -F"|" 'gsub("#",FS,$0) 1' inputfile | nawk -F"|" '! a[$3]++'

Regards

A.D
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines with Pattren Matching

Hi , I need to remove the lines that matches the pattern TABLEEXCLUDE *.AQ$_*_F ; * is wildcard, it can be any word. For example, I have following file: TABLEEXCLUDE THOT.AQ$_PT_ADDR_CLEANUP_QTAB2_F ; TABLEEXCLUDE THOT.AQ$_MICRO_SERVICE_QT_F ; TEST TABLEEXCLUDE... (1 Reply)
Discussion started by: rcc50886
1 Replies

2. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

3. Shell Programming and Scripting

Remove lines containing 2 or more duplicate strings

Within my text file i have several thousand lines of text with some lines containing duplicate strings/words. I would like to entirely remove those lines which contain the duplicate strings. Eg; One and a Two Unix.com is the Best This as a Line Line Example duplicate sentence with the word... (22 Replies)
Discussion started by: martinsmith
22 Replies

4. Linux

Remove matching files from a list

hi, i have a cache file with below file list more gtda_11.cache GTDA_Dly_Pmix_GB_6_20130624.20130624070610.psv GTDA_Dly_Pmix_CH_006_20130624.20130624140018.psv GTDA_Dly_Pmix_GB_6_20130624.20130624070620.psv GTDA_Dly_Pmix_BE_6_20130624.20130624070620.psv... (2 Replies)
Discussion started by: renuk
2 Replies

5. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

6. Shell Programming and Scripting

Compare one files with strings from another + remove lines

Have two files and want to compare the content of file1 with file2. When matched remove the line. awk 'NR==FNR {b; next} !(b in $0)' file1 file2file1 1. if match 2. removefile2 1. this line has to be removed if match 2. this line has a match, remove 3. this line has no match, no removingThe... (3 Replies)
Discussion started by: sdf
3 Replies

7. Shell Programming and Scripting

How to remove lines containing strings

Hi Guys, I need some script in removing lines containing strings like ",, ," and "rows". Retain only numbers as the output. See below for the input and output file. INPUT FILE: 9817 9832 6285 6312 6313 6318 ,, , 6329 7078 7098 7130 7959 7983 (7 Replies)
Discussion started by: pinpe
7 Replies

8. Shell Programming and Scripting

Remove duplicate lines (the first matching line by field criteria)

Hello to all, I have this file 2002 1 23 0 0 2435.60 131.70 5.60 20.99 0.89 0.00 285.80 2303.90 2002 1 23 15 0 2436.60 132.90 6.45 21.19 1.03 0.00 285.80 2303.70 2002 1 23 ... (6 Replies)
Discussion started by: joggdial3000
6 Replies

9. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

10. Shell Programming and Scripting

different take on common ?: search for two strings and remove lines between them

Thank you for assisting, I've got a partial solution just needs a tweak. Hulk-BASH$ cat somefile.txt oh there is some stuff here some more stuff here START_LABEL stuff I want more stuff I want END_LABEL other stuff here too and even more stuff here too Hulk-BASH$ Hulk-BASH$ sed... (8 Replies)
Discussion started by: laser
8 Replies
Login or Register to Ask a Question