Delete the lines if it matchs a selected string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete the lines if it matchs a selected string
# 1  
Old 11-13-2010
Delete the lines if it matchs a selected string

HI,

I need one help to generate the file based on few condition,
I need to print only those line which has N and delete the line which as all Y's. ANd I need to compare only the fields which are marked as Y and N .
And one more thing is in the below file 1 and file 2 and file2 abc, dbc can come in any of the fields,

for exp , file1
Code:
abc|Y|Y|Y|
bdc|N|Y|Y|
dfg|Y|N|Y|
hjk|Y|Y|Y

output of file1
Code:
bdc|N|Y|Y|
dfg|Y|N|Y|

file2
Code:
Y|Y|abc|Y|
N|Y|bdc|Y|
Y|N|dfg|Y|
Y|Y|hjk|Y|

output of file2
Code:
N|Y|bdc|Y|
Y|N|dfg|Y|

But I need to compare only Y and N fields.

Can you please help me out.

Thanks in Advance
Rashmi

Last edited by Scott; 11-13-2010 at 01:45 PM.. Reason: Code tags
# 2  
Old 11-13-2010
try this:

Code:
 
egrep '\|N$|^N\||\|N\|' filename > newfilename

# 3  
Old 11-13-2010
thanks a lot, it works ...I had one more doubt, wht if the field delimiter is not fixed i.e what if I had to process multiple files without knowing the filed delimiter .
Can I pass the field delimiter during the runtime.
in the below example, my file1 has pipe delimiter and file2 has tab delimter.

file1
abc|Y|N|
cdb|Y|Y

and file2
abc Y N
cdb Y Y
# 4  
Old 11-14-2010
can try this:

assign a shell variable as below

Code:
 
delimit="\|"  ##considering it is a pipe here, as | is a reg exp metacharacter we have to escape that.

then use egrep as below

Code:
 
egrep "${delimit}"'N$|^N'"${delimit}"'|'"${delimit}"'N'"${delimit}" filename > newfilename

# 5  
Old 11-14-2010
Hi,

Try this

Code:
sed "/Y|Y|Y\|Y|Y|.*|Y/d" file*

And if you want to modify the file content just add -i option to sed.
# 6  
Old 11-14-2010
Code:
grep N file*

# 7  
Old 11-14-2010
thanks this works egrep '\|N$|^N\||\|N\|' file. Is it possible to make it read from the 2nd line, as my 1st line of the file is a header.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines from file matching a string

I wish to search and delete all lines in /app/Jenkins/deploy.txt having this filename string /app/Jenkins/file2.mrt as entry: I'm using : colon as delimiter in sed command as I'm dealing with file paths. Below is the command I was expecting to work. sed -i ":/app/Jenkins/file2.mrt:d"... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Delete all lines except a line starting with string

Shell : bash OS : RHEL 6.8 I have a file like below. $ cat pattern.txt hello txt1 txt2 txt3 some other text txt4 I want to remove all lines in this file except the ones starting with txt . How can I do this ? (4 Replies)
Discussion started by: omega3
4 Replies

3. Shell Programming and Scripting

How to delete lines starting with specific string?

Dear all, I would like to delete even lines starting with "N" together with their respective titles which are actually odd lines. Below is the example of input file. I would like to remove line 8 and 12 together with its title line, i.e., line 7 and 11, respectively.... (2 Replies)
Discussion started by: huiyee1
2 Replies

4. Shell Programming and Scripting

Delete selected lines

hi Gurus, I have a source file with more than 10 columns ( not fixed ) I want to delete all the lines on the following condition 1) where i have first column as "UPDATE PLAN ADD RATE SCHEDULE" and fourth column as null awk '($1=="UPDATE PLAN ADD RATE SCHEDULE" && $4=="") {print $0}'... (5 Replies)
Discussion started by: r_t_1601
5 Replies

5. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

delete all lines with string, process all files in directory

Simply, I have a directory of text files and I need to delete every line in every file containing a specific string. I want to write the modified files to an empty sub directory. I can't seem to get the sed command to delete the lines containing the string, and not just the string, in other... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

7. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

8. Shell Programming and Scripting

How to delete selected string from a file?

awk '!(/^$/||/--/||/selected/||/^ *$/){print "A." $1 " <> B." $1 " or"}' infile my AWK out put is : A.KZ <> B.KZ or A.KZT <> B.KZT or A.KZ_Z <> B.KZ_Z or A.LH <> B.LH or A.MAN<> B.MAN or A.OBJEKT <> B.OBJECT or A.PAK <> B.PAK ; is there any way to controle AWK to not print the... (1 Reply)
Discussion started by: kanakaraju
1 Replies

9. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

10. UNIX for Dummies Questions & Answers

Delete multiple lines containting a variable string using SED.

Good morning, Novice scripter in Unix here, and I've run into and sed task I can't quite wrap my head around. I'm pulling my hair out fast enough as it is and thought I would go to the knowledge bank. I have a sorted file that I'm trying to trim down by deleting any line whose first few... (2 Replies)
Discussion started by: selkirk
2 Replies
Login or Register to Ask a Question