Awk; pattern match, remove and re write


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk; pattern match, remove and re write
# 1  
Old 03-09-2015
Awk; pattern match, remove and re write

the following pattern match works correctly for me

Code:
awk '/name="Fruits"/{f=1;next} /"name=Vegetables"/{f=0} f' filename


This works well for me. Id like to temporarily move the match out of the file ( > newfile) and be able to stick it back in the same place at a later time.

Is this possible?

Last edited by Chubler_XL; 03-09-2015 at 06:20 PM.. Reason: Add code tags
# 2  
Old 03-09-2015
not tested:
Code:
awk '
  FNR==NR { newf[++c]=$0; next }

/name="Fruits"/{f=1}
f && /"name=Vegetables"/ {
  for(i=1; i in newf; i++)
     print newf[i]
  f=0
}
1
' newfile filename > newNewFile

# 3  
Old 03-09-2015
that is interested and I will certainly test. Follow up question if I may. What if I would like to put a place holder while the text is removed from the file. Perhaps a comment like


###
##--date was removed here
####
# 4  
Old 03-09-2015
Code:
awk '
  FNR==NR { newf[++c]=$0; next }

/date was removed here/ {
  for(i=1; i in newf; i++)
     print newf[i]
  next
}
1
' newfile filename

# 5  
Old 03-10-2015
Quote:
Originally Posted by vgersh99
not tested:
Code:
awk '
  FNR==NR { newf[++c]=$0; next }

/name="Fruits"/{f=1}
f && /"name=Vegetables"/ {
  for(i=1; i in newf; i++)
     print newf[i]
  f=0
}
1
' newfile filename > newNewFile



this does not seem to work
# 6  
Old 03-10-2015
Quote:
Originally Posted by TY718
that is interested and I will certainly test. Follow up question if I may. What if I would like to put a place holder while the text is removed from the file. Perhaps a comment like


###
##--date was removed here
####
You mean you want two output files - the "Fruits" file and the original file with the fruits - not the header "Fruits" - removed and the placeholder added?
If yes, you need to provide for this in your original code, and then you can use vgersh99's proposal to join the two files back.
# 7  
Old 03-10-2015
is this a better explanation?


a. Match text between two patterns(including pattern)

b. Move match out to new file

c. Replace text in original file with comment (i.e. ##insert data here##)

d. Move back in to original file at same location at later date
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to remove pattern and lines above pattern

In the awk below I am trying to remove all lines above and including the pattern Test or Test2. Each block is seperated by a newline and Test2 also appears in the lines to keep but it will always have additional text after it. The Test to remove will not. The awk executed until the || was added... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

3. Shell Programming and Scripting

Remove multiple lines that match pattern

Not sure how I can accomplish this. I would like to remove all interfaces that have the commands I would like to see: switchport port-security, spanning-tree portfast. One line is no problem. interface FastEthernet0/8 spanning-tree portfast interface FastEthernet0/9 spanning-tree... (4 Replies)
Discussion started by: mrlayance
4 Replies

4. Shell Programming and Scripting

[Solved] Pattern match and write to separate files

I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2). Mapping for pattern match and add line : for Alpha 123 for Beta 234 for Gamma 345 no match (goes into another file) File 1 ... (3 Replies)
Discussion started by: w020637
3 Replies

5. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

6. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Remove lines from batch of files which match pattern

I need to remove all lines which does not match the pattern from a text file (batch of text files). I also need to keep the header line which is the first line of the file. Please can you provide an example for that. I used this to solve half of my work. I was unable to keep the first line of... (3 Replies)
Discussion started by: talktobog
3 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

want to pattern match using awk

Hello Friends, My script gives an output like below:- but i only want the red part to be displayed. how to i do that. I am enclosing my shell script after that. id='CCRCWebServerINSTALLDIR' id='AdministrationTools-CINSTALLDIR' id='AdministrationTools-ent-CINSTALLDIR'... (3 Replies)
Discussion started by: asirohi
3 Replies
Login or Register to Ask a Question