Change nth depending on matiching a pattern in Y line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change nth depending on matiching a pattern in Y line
# 1  
Old 12-03-2013
Change nth depending on matiching a pattern in Y line

Hi,

I have below file, each line that starts with /* marks the beginning of the a new job.
Code:
   /* ----------------- cmdsMlyMoveTPMPLANTJ -----------------
UNIX_JOB CMMM002J
  CMDNAME /home2/proddata/bin/moveTPMPLANT.sh
  AGENT CMDSHP
  USER proddata
  AFTER CMMU001J
  /* "DDRG monthly processing. This job can FAIL if the files are n
  ENVAR STDOUT=/tmp/cmdsMlyMoveTPMPLANTJ
  ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily
ENDJOB

  /* ----------------- cmdsMlyDdrgRcvF -----------------
FILE_TRIGGER CMMD003F
  AGENT CMDSHP
  USER autosys
  AFTER CMMM002J
  /* "watch prepareDDRG.data(www, xtmp.htm, Tpmplant.exe"
  FILENAME /home3/ref_files/download/prepareDDRG.data
  ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily
ENDJOB

  /* ----------------- cmdsMlyMoveTPMPLANTJ -----------------
UNIX_JOB CMMM002J
  CMDNAME /home2/proddata/bin/moveTPMPLANT.sh
  AGENT CMDSHP
  USER proddata
  AFTER CMMU001J
  /* "DDRG monthly processing. This job can FAIL if the files are n
  ENVAR STDOUT=/tmp/cmdsMlyMoveTPMPLANTJ
  ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily
ENDJOB

i want to get rid of the line ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily whereever the new job starts with FILE_TRIGGER. Please assist

Last edited by Franklin52; 12-04-2013 at 03:10 AM.. Reason: Replace icode tags with code tags
# 2  
Old 12-03-2013
Hi,
Try:
Code:
sed -n -e '/\/\*/,/ENDJOB/{/FILE_TRIGGER/H' -e '/ENVAR ENV/!b' -e 'x' -e '/FILE_TRIGGER/!b' -e 'x' -e 'p' -e 'x' -e '}' file

Regards.
# 3  
Old 12-03-2013
Here is an awk solution:

Code:
awk '
  $2 ~ "^FILE_TRIGGER" {gsub("\nENVAR ENV=[^\n]*\n","")}
  NF{printf("%s", "/* -"$0)}' RS='/[*] -' FS='\n\n' infile

# 4  
Old 12-03-2013
Code:
>>cat test2.sh
awk '
$2 ~ "^FILE_TRIGGER" {gsub("\nENVAR ENV=[^\n]*\n","")}
NF{printf("%s", "/* -"$0)}' RS='/[*] -' FS='\n\n' txt
 
>>./test2.sh
awk: syntax error near line 2
awk: bailing out near line 2

---------- Post updated at 07:09 PM ---------- Previous update was at 07:07 PM ----------

Sed didnt work either it just displayed one line

Code:
>>sed -n -e '/\/\*/,/ENDJOB/{/FILE_TRIGGER/H' -e '/ENVAR ENV/!b' -e 'x' -e '/FILE_TRIGGER/!b' -e 'x' -e 'p' -e 'x' -e '}' txt
ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily

---------- Post updated at 07:15 PM ---------- Previous update was at 07:09 PM ----------

Just to be clear, I want my output to look like this

Code:
 
/* ----------------- cmdsMlyMoveTPMPLANTJ -----------------
UNIX_JOB CMMM002J
CMDNAME /home2/proddata/bin/moveTPMPLANT.sh
AGENT CMDSHP
USER proddata
AFTER CMMU001J
/* "DDRG monthly processing. This job can FAIL if the files are n
ENVAR STDOUT=/tmp/cmdsMlyMoveTPMPLANTJ
ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily
ENDJOB
 
/* ----------------- cmdsMlyDdrgRcvF -----------------
FILE_TRIGGER CMMD003F
AGENT CMDSHP
USER autosys
AFTER CMMM002J
/* "watch prepareDDRG.data(www, xtmp.htm, Tpmplant.exe"
FILENAME /home3/ref_files/download/prepareDDRG.data
ENDJOB
 
/* ----------------- cmdsMlyMoveTPMPLANTJ -----------------
UNIX_JOB CMMM002J
CMDNAME /home2/proddata/bin/moveTPMPLANT.sh
AGENT CMDSHP
USER proddata
AFTER CMMU001J
/* "DDRG monthly processing. This job can FAIL if the files are n
ENVAR STDOUT=/tmp/cmdsMlyMoveTPMPLANTJ
ENVAR ENV=/home2/autosys/cmdsprod/profiles/daily
ENDJOB

this example has just one file trigger my real file has multiple of these and are all in between UNIX_JOB

Last edited by Franklin52; 12-04-2013 at 03:12 AM.. Reason: Replace icode tags with code tags
# 5  
Old 12-03-2013
You're using Solaris. Try /usr/xpg4/bin/awk or nawk inplace of awk
# 6  
Old 12-03-2013
Code:
>>cat test2.sh
/usr/xpg4/bin/awk '
$2 ~ "^FILE_TRIGGER" {gsub("\nENVAR ENV=[^\n]*\n","")}
NF{printf("%s", "/* -"$0)}' RS='/[*] -' FS='\n\n' txt

OUTPUT-------

Code:
/* -  /* -* ----------------- cmdsMlyMoveTPMPLANTJ -----------------
UNIX_JOB CMMM002J
  CMDNAME /* -home2/* -proddata/* -bin/* -moveTPMPLANT.sh
  AGENT CMDSHP
  USER proddata
  AFTER CMMU001J
  /* -* "DDRG monthly processing. This job can FAIL if the files are n
  ENVAR STDOUT=/* -tmp/* -cmdsMlyMoveTPMPLANTJ
  ENVAR ENV=/* -home2/* -autosys/* -cmdsprod/* -profiles/* -daily
ENDJOB

  /* -* ----------------- cmdsMlyDdrgRcvF -----------------
FILE_TRIGGER CMMD003F
  AGENT CMDSHP
  USER autosys
  AFTER CMMM002J
  /* -* "watch prepareDDRG.data(www, xtmp.htm, Tpmplant.exe"
  FILENAME /* -home3/* -ref_files/* -download/* -prepareDDRG.data
  ENVAR ENV=/* -home2/* -autosys/* -cmdsprod/* -profiles/* -daily
ENDJOB

  /* -* ----------------- cmdsMlyMoveTPMPLANTJ -----------------
UNIX_JOB CMMM002J
  CMDNAME /* -home2/* -proddata/* -bin/* -moveTPMPLANT.sh
  AGENT CMDSHP
  USER proddata
  AFTER CMMU001J
  /* -* "DDRG monthly processing. This job can FAIL if the files are n
  ENVAR STDOUT=/* -tmp/* -cmdsMlyMoveTPMPLANTJ
  ENVAR ENV=/* -home2/* -autosys/* -cmdsprod/* -profiles/* -daily
ENDJOB


Last edited by Franklin52; 12-04-2013 at 03:12 AM.. Reason: Replace icode tags with code tags
# 7  
Old 12-04-2013
Sorry I thought your input file was double spaced (blank line between each data line)

You should use [code] and [/code] tags around your data not [icode] and [/icode].

Also are some of those lines indented with spaces, it looks like they might be...This should work a little better:

Code:
/usr/xpg4/bin/awk '
  $2 ~ "^ *FILE_TRIGGER"{gsub(" *ENVAR ENV=/home2/[^\n]*\n","")}
  NF{printf "%s","/* -"$0}' RS='/[*] -' FS='\n' infile


Last edited by Chubler_XL; 12-04-2013 at 12:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to display when nth line match a pattern?

Hi All, I have sample of listing as following Database 2 entry: Database alias = PXRES Database name = PXRES Local database directory = /db2/data1/db2phnx Database release level = d.00 Comment ... (3 Replies)
Discussion started by: ckwan
3 Replies

2. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

3. UNIX for Dummies Questions & Answers

Printing nth and n+1th line after a pattern match

Hi , I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed :) (6 Replies)
Discussion started by: saj
6 Replies

4. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

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

6. Shell Programming and Scripting

Getting filename for Nth line pattern match

Hi, I have many scripts in particular directory. And few of the scripts have exit 0 in second line. Now i wanted to list out the scripts name which has the exit 0 in its second line I tried many options , but i can not get the filename along with the nth line pattern match :mad:. Can anyone... (14 Replies)
Discussion started by: puni
14 Replies

7. Shell Programming and Scripting

How to change variable name depending on iteration

Hi, I would like to get a solution to this situation if possible. In a "for" loop how can I assign a value to a variable and then change the name of that variable depending on the loop iteration? So what I am looking for is something like this in pseudo-code: for i in 1 2 3 4 do echo... (2 Replies)
Discussion started by: yonderboy
2 Replies

8. Shell Programming and Scripting

To filter a certain pattern depending on neighbour

Hi, As I am new to filtering section of Unix I am facing a problem I have following lines in text file : Created RELEASE in dist/Apple_release_1_0_.zip Created RELEASE in dist/Banana_release_1_0_.zip Created RELEASE in dist/Mango_release_1_0_.zip Created RELEASE in... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

9. Shell Programming and Scripting

change line found by pattern using sed

I want to change a line like CPM_THRESHOLD 0.8 // to a new value using sed I am trying sed -i "s/CPM_THRESHOLD/CPM_THRESHOLD\t$COH\t\t\/\//" $INPUT_4 but how can i substitute the whole line begining with CPM_THRESHOLD and substitute it? (2 Replies)
Discussion started by: larne
2 Replies

10. Shell Programming and Scripting

cut subsrting from string depending on pattern

Dears, i'm glad to be a member of this big UNIXer's group :( ,hoping to be one of'm. my question: how i get the subsrting ahmad "from" the string ".ahmad." for example ?? Thank you all. (5 Replies)
Discussion started by: Muslem_it
5 Replies
Login or Register to Ask a Question