-1
#!/bin/bash

NEW_TEXT="if failed Send to Karn Kumarl"

OLD_FILE="$(awk '{print $1}' RMANJOBS | while read JB;do autorep -j $JB -q;done | egrep "^/|^insert_job|^description" | sed '0~3 a\\')"

NEW_FILE="MRMANJOBS"

AWK='''
    #.. Get new text from the shell variable.
    BEGIN { NewText = " " ENVIRON["TXT"] "\042"; }
    #.. If this line needs the fix, substitute the text.
    /^description:.* [Ii]f failed / { sub (/ [Ii]f failed .*$/, NewText); }
    #.. Print all lines, whether fixed or not.
    { print; }
    '''

TXT="${NEW_TEXT}" awk "${AWK}" "${OLD_FILE}" > "${NEW_FILE}"

I need to sort the command output into a variable .. as its not taking for OLD_FILE.

  • Does it work if you just get rid of the outer quotes? They aren't necessary and might be messing you up – Eric Renouf Jul 27 '15 at 18:22
  • @Eric - Even it doesn't work using without quotes like $().. While i am executing this script it simply display the command line output on the screen, while it should hold it and put it into NEW_FILE. – karn Jul 27 '15 at 18:43
  • Well, my standard next advice for this would be to put set -x at least before the line that's giving you trouble so you can see what it looks like when the variables are all expanded, perhaps you'll spot the issue that way. – Eric Renouf Jul 27 '15 at 18:51
  • I kept the set -x but it doesn't shows any issues though, Just prints the output of OLD_FILE on the screen. Only error i can see is "for reading (File name too long)" in the output. – karn Jul 27 '15 at 19:18
  • Can you show what is in OLD_FILE variable? (the output of awk '{print $1}' RMANJOBS | while read JB;do autorep -j $JB -q;done | egrep "^/|^insert_job|^description" | sed '0~3 a\\' command). Why you put empty lines after each 3rd? – Costas Jul 27 '15 at 19:30
0
#!/bin/bash

while read JB last
do
    autorep -j $JB -q
done < RMANJOBS |
sed -rn "/^\/|^insert_job|^description/{
    s/(description:.* [Ii]f failed )[^"]*/\1Send to Karn Kumarl/
    s/insert/update/
    p
    }" |
sed '0~3 G' > MRMANJOBS
  • @Costas- Can you explain Please – karn Jul 27 '15 at 22:23
  • This new script does not work .. it throws error for sed.. sed: -e expression #1, char 4: unknown command: `|' – karn Jul 27 '15 at 22:44
  • @karn Sorry I'd make / escaped. Test repaired. – Costas Jul 28 '15 at 7:52
  • @Costas 1- Though the solution working fine but here we forgot to "awk '{print $1}' RMANJOBS" Part to print the $1 as this has many feilds. 2- We need the last word to be closed with double quotes as below.<br/> description: "Unmount TCFTP filesystems on ABC - if failed Send to Karn Kumarl" <br/> 3- Can we replace the string altogether with sed in the below line which you suggested...<br/> sed -rn "/^\/|^insert_job|^description/ <br/> --> I need string "insert" to be replaced with "update" – karn Jul 28 '15 at 14:24
  • @karn 1/ You need not use awk due to read JB last which put in JB just 1st field. 2/ As I understand quotes there are in line already? 3/ I'm not sure to clear understand what you need. – Costas Jul 28 '15 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.