Delete line by pattern match in column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete line by pattern match in column
# 1  
Old 06-30-2015
Delete line by pattern match in column

Code:
file:
1|12322|tow|
5|23422|pow|
6|23423|cow|
3|34324|how|

deletelines:
12322
23423

My command to delete line
Code:
while read NUM 
do 
awk -F"\|"  '$2 !~ /`"$NUM"`/' file >file.back 
mv file.back file 
done<deletelines

But NUM variable is not working, unable to delete the line
Can some one help on this?
# 2  
Old 06-30-2015
On top of single quotes preventing the shell's expansion of variables, that's not the right way to supply parameters to awk. And, the above use of backticks is not clear to me. Try
Code:
awk -F"\|" -vNM=$NUM "\$2 !~ NM" file

But, wouldn't it be much better NOT to run awk n times, and NOT to read file n times?
Look at this if it does what you need:
Code:
awk -F"\|"  'NR==FNR {T[$1]; next} $2 in T {next} 1' deletelines  file
5|23422|pow|
3|34324|how|

# 3  
Old 06-30-2015
Code:
awk -F\| 'NR==FNR{A[$1];next}!($2 in A)' deletelines file > file.back

# 4  
Old 06-30-2015
Thanks Rudic and Yoda for your help !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If pattern match in other column, modify column 3.

My command sed will modify everything in column 3 if i will use the command below. I want to search for a pattern then modify everything in column 3. sed -i 's/\|165\|/server1/g' file.txt Input: 01-31-2019 19:14:05|device|165|1548962040165|5c5348f9-0804-1111|file_attach|7271|587|smtp|... (6 Replies)
Discussion started by: invinzin21
6 Replies

2. Shell Programming and Scripting

How to delete the previous line after pattern match?

Team, I am writing a shell script to perform few health checks of the system, where I need to delete the previous line in the text file after pattern match using sed (or) awk. Could you please help me out on this? For example, <td> <td style=color:green align=center> </td> </tr>... (6 Replies)
Discussion started by: Nagaraj R
6 Replies

3. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

4. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

5. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

6. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

7. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

8. Shell Programming and Scripting

delete block of lines when pattern does not match

I have this input file that I need to remove lines which represents more than 30 days of processing. Input file: On 11/17/2009 at 12:30:00, Program started processing...argc=7 Total number of bytes in file being processed is 390 Message buffer of length=390 was allocated successfully... (1 Reply)
Discussion started by: udelalv
1 Replies

9. Shell Programming and Scripting

delete a line that does not match the pattern

hi, i am parsing a file, in that searching for lines those contains "$threadNo.Received message:" , if that line contains the required fields write them into a separate file other wise ignore them. i am using the following code,but it is printing all the lines , i dont want to rpint , please help... (3 Replies)
Discussion started by: Satyak
3 Replies

10. UNIX for Dummies Questions & Answers

How to delete lines do NOT match a pattern

On Unix, it is easy to get those lines that match a pattern, by grep pattern file or those lines that do not, by grep -v pattern file but I am editing a file on Windows with Ultraedit. Ultraedit support regular expression based search and replace. I can delete all the lines that match a... (1 Reply)
Discussion started by: JumboGeng
1 Replies
Login or Register to Ask a Question