awk script to move a line after the matched pattern line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to move a line after the matched pattern line
# 1  
Old 06-02-2010
awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices
after that the explanantion and finally the answer.

Code:
1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become 
obsolete has been declining because of the introduction of advanced manufacturing technology (AMT). 
Given the rate at which AMT is currently being introduced in manufacturing, the average  worker’s 
old skills become obsolete and new skills are required within as little as five years.
 
Which of the following plans, if feasible, would allow a company to prepare  most effectively for 
the rapid obsolescence of skills described  above?
 
A. The company will develop a program to offer selected employees the opportunity to receive training 
six years after they were originally hired.
B. The company will increase its investment in AMT every year for a period of at least five years.
C. The company will periodically survey its employees to determine how the introduction of AMT has 
affected them.
D. Before the introduction of AMT, the company will institute an educational program to inform its 
employees of the probable consequences of the introduction of AMT.
E. The company will ensure that it can offer its employees any training necessary  for meeting their 
job requirements.
 
Explanantion
 
A Providing training only to selected employees and only after their skills have already become obsolete 
is not likely to  be an elective response.
B  This plan only accelerates the problem and does not address the employees’ skills.
C Periodic surveys may provide information to employers but will not be enough to prevent employees’ 
skills from becoming  obsolete.
D Having knowledge of the consequences does not prevent those consequences; employees’ skills will still 
become obsolete.
E  Correct. This would ensure that all employees have the most current occupational knowledge and skills 
needed for their  jobs
 
Answer:   C.

And i want to move the line which starts with pattern 'Answer:' right after the final choice pattern 'E.' like this

Code:
 
1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete 
has been declining because of the introduction of advanced manufacturing technology (AMT). Given the rate 
at which AMT is currently being introduced in manufacturing, the average  worker’s old skills become 
obsolete and new skills are required within as little as five years.
 
Which of the following plans, if feasible, would allow a company to prepare  most effectively for the rapid 
obsolescence of skills described  above?
 
A. The company will develop a program to offer selected employees the opportunity to receive training six 
years after they were originally hired.
B. The company will increase its investment in AMT every year for a period of at least five years.
C. The company will periodically survey its employees to determine how the introduction of AMT has affected 
them.
D. Before the introduction of AMT, the company will institute an educational program to inform its employees 
of the probable consequences of the introduction of AMT.
E.  The company will ensure that it can offer its employees any training necessary  for meeting their job 
requirements.
 
Answer:   C.
 
Explanantion
 
A Providing training only to selected employees and only after their skills have already become obsolete is 
not likely to  be an elective response.
B  This plan only accelerates the problem and does not address the employees’ skills.
C Periodic surveys may provide information to employers but will not be enough to prevent employees’ skills 
from becoming  obsolete.
D Having knowledge of the consequences does not prevent those consequences; employees’ skills will still 
become obsolete.
E  Correct. This would ensure that all employees have the most current occupational knowledge and skills 
needed for their  jobs

any awk script which does the above job, please?

Last edited by nanchil_guy; 06-02-2010 at 08:55 AM.. Reason: more formatting required
# 2  
Old 06-02-2010
You can give this a try:
Code:
awk '
/Explanantion/{f=1}
/Answer:/{print $0 RS RS s; s=""; f=0;next}
f{s=s?s RS $0:$0; next}
1' file

# 3  
Old 06-02-2010
Thanks Franklin for the quick response, worked like a charm!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

4. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

5. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

6. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

7. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

8. Shell Programming and Scripting

Use AWK to move matched line back one?

Can somebody help me with this? I'm sure it's a no-brainer if you know awk... but I don't. Input: Blah Blah Me love you long time Blah Blah awk magic with 'long time' ==> Output: Blah Blah Me love you long time (0 Replies)
Discussion started by: Ryan.
0 Replies

9. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

10. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies
Login or Register to Ask a Question