Help with replace line based on specific pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with replace line based on specific pattern match
# 1  
Old 11-25-2011
Help with replace line based on specific pattern match

Input file
Code:
data20714	7327	7366	detail	
data20714	7327	7366	main	
data250821	56532	57634	detail	
data250821	57527	57634	main	
data250821	57359	57474	main	
data250821	57212	57301	main	
data250821	57140	57159	detail
data250821	56834	57082	main	
data250821	56708	56779	main	
data250821	56532	56652	main	
.
.

Desired output file:
Code:
data20714	7327	7366	main	

data250821	57527	57634	main	
data250821	57359	57474	main	
data250821	57212	57301	main	

data250821	56834	57082	main	
data250821	56708	56779	main	
data250821	56532	56652	main	
.
.

My purpose is to replace the whole line with empty content when found that column 4 is "detail".
Thanks for any advice.
# 2  
Old 11-25-2011
Code:
$ nawk '{if($4~/detail/){$0="";print}else{print $0}}' infile

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 11-25-2011
Code:
$
$
$ cat f24
data20714       7327    7366    detail
data20714       7327    7366    main
data250821      56532   57634   detail
data250821      57527   57634   main
data250821      57359   57474   main
data250821      57212   57301   main
data250821      57140   57159   detail
data250821      56834   57082   main
data250821      56708   56779   main
data250821      56532   56652   main
$
$
$
$ perl -plane '$_="" if $F[3] eq "detail"' f24

data20714       7327    7366    main

data250821      57527   57634   main
data250821      57359   57474   main
data250821      57212   57301   main

data250821      56834   57082   main
data250821      56708   56779   main
data250821      56532   56652   main
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 4  
Old 11-25-2011
Code:
 
nawk '/detail/{print "";next}! /detail/{print}' input

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Replace the line with specific pattern

Hello All I'm trying to change one string from a file contening this patern: xxxx-xxxx 4 numbers - end 4 other numbers This is a sample of the file: LDR 00679 am a2200205 4500 =001 3617 =008 030219s2000\\\\xxx|||||\||||\00|\0\spa\d =020 \\$a0211-1942 =041 \\$aCastellà =093 ... (5 Replies)
Discussion started by: ldiaz2106
5 Replies

3. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

4. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

5. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

6. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

7. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

8. Shell Programming and Scripting

Concatenating and appending string based on specific pattern match

Input #GEO-1-type-1-fwd-Initial 890 1519 OPKHIJEFVTEFVHIJEFVOPKHIJTOPKEFVHIJTEFVOPKOPKHIJHIJHIJTTOPKHIJHIJEFVEFVOPKHIJOPKHIJOPKEFVEFVOPKHIJHIJEFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #GEO-1-type-2-fwd-Terminal 1572 2030... (7 Replies)
Discussion started by: patrick87
7 Replies

9. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question