Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Removing part of string in unix script

I need to remove the first characters of this script in unix. This is the string i get (its always the same until the first "15", thats why i need to discard the first characters):

The parts in bold letters need to be removed

44 *-* ADDRESS TSO "SUBCOM asdfd" +++ RC(-3) +++ ------------------------------------------------------------- - QUERY EJECUTADA - ------------------------------------------------------------- SELECT ASDF(RIGHT(A,B,C),2) AS PROCESS, STATUS_SBP, COUNT(*) AS NUM FROM A.B WHERE SERVICE= '000 12345' AND SYSTEM= 'ASD' AND LAST_MESSAGE> TIMESTAMP(CURRENT DATE,' 00:00:00') GROUP BY LEFT(RIGHT(ASDF,12),2),STATUS------------------------------------------------------------- 15 B1 60 15 1C 11 15 1Q 2 15 1R 2406 16 B1 61 16 1R 1024 16 1S 99 ------------------------------------------------------------- - COMMIT EXECUTED- -------------------------------------------------------------
0
celtician
Asked:
celtician
  • 5
  • 4
1 Solution
 
ozoCommented:
#!/bin/bash
STRING=$(cat <<END
44 *-* ADDRESS TSO "SUBCOM asdfd" +++ RC(-3) +++ ------------------------------------------------------------- - QUERY EJECUTADA - ------------------------------------------------------------- SELECT ASDF(RIGHT(A,B,C),2) AS PROCESS, STATUS_SBP, COUNT(*) AS NUM FROM A.B WHERE SERVICE= '000 12345' AND SYSTEM= 'ASD' AND LAST_MESSAGE> TIMESTAMP(CURRENT DATE,' 00:00:00') GROUP BY LEFT(RIGHT(ASDF,12),2),STATUS------------------------------------------------------------- 15 B1 60 15 1C 11 15 1Q 2 15 1R 2406 16 B1 61 16 1R 1024 16 1S 99 ------------------------------------------------------------- - COMMIT EXECUTED- -------------------------------------------------------------
END
)
STRING=${STRING#*=*- }
STRING=${STRING%%-*}
echo $STRING
0
 
celticianAuthor Commented:
I might not be initialiating the variable:

When i execute i get:

testscript.ksh[20]: testfile.txt={testfile.txt#*=*-:  not found
testscript.ksh[21]: testfile.txt={testfile.txt%%-*}:  not found


And this is my code (testscript.ksh)

First part of the file: (line number between parenthesis)

#!/bin/ksh

MAIL_FILE="testfile.txt"

rm -f $MAIL_FILE

(17) cat /var/opt/a/b/c/FILE_NAME >> $MAIL_FILE

(20)$MAIL_FILE={$MAIL_FILE#*=*- }
(21)$MAIL_FILE={$MAIL_FILE%%-*}


cat $MAIL_FILE|mail ME@domain.com
0
 
ozoCommented:
MAIL_FILE=`cat /var/opt/a/b/c/FILE_NAME`
MAIL_FILE=${MAIL_FILE#*=*- }
echo ${MAIL_FILE%%-*} |mail ME@domain.com
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
celticianAuthor Commented:
I keep getting:

testscript.ksh[8]: cat /var/opt/a/b/c/file_name: cannot create
testscript.ksh[9]: cat /var/opt/a/b/c/file_name: cannot create
testscript.ksh[10]: cat /var/opt/a/b/c/file_name: cannot create
testscript.ksh[11]: cat /var/opt/a/b/c/file_name: cannot create
testscript.ksh[12]: cat /var/opt/a/b/c/file_name: cannot create

Where these lines in the code are some echoes:

echo "From:Script" > $MAIL_FILE
echo "To:Me Me <me@company.com>" >> $MAIL_FILE
echo "Subject:Test eMail" >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE

This was working before the previous changes
0
 
ozoCommented:
if /var/opt/a/b/c/file_name contains
44 *-* ADDRESS TSO "SUBCOM asdfd" +++ RC(-3) +++ ------------------------------------------------------------- - QUERY EJECUTADA - ------------------------------------------------------------- SELECT ASDF(RIGHT(A,B,C),2) AS PROCESS, STATUS_SBP, COUNT(*) AS NUM FROM A.B WHERE SERVICE= '000 12345' AND SYSTEM= 'ASD' AND LAST_MESSAGE> TIMESTAMP(CURRENT DATE,' 00:00:00') GROUP BY LEFT(RIGHT(ASDF,12),2),STATUS------------------------------------------------------------- 15 B1 60 15 1C 11 15 1Q 2 15 1R 2406 16 B1 61 16 1R 1024 16 1S 99 ------------------------------------------------------------- - COMMIT EXECUTED- -------------------------------------------------------------
then
MAIL_FILE=`cat /var/opt/a/b/c/FILE_NAME`
MAIL_FILE=${MAIL_FILE#*=*- }
MAIL_FILE=${MAIL_FILE%%-*} 

Open in new window

would set $MAIL_FILE to '15 B1 60 15 1C 11 15 1Q 2 15 1R 2406 16 B1 61 16 1R 1024 16 1S 99 '
Which I don't think is something you would want as the destination of a >>

Maybe we should not be using $MAIL_FILE for this, and go back to using $STRING:
MAIL_FILE="testfile.txt"
STRING=$(cat /var/opt/a/b/c/FILE_NAME)
STRING=${STRING#*=*- }
echo ${STRING%%-*} >> $MAIL_FILE 

Open in new window

0
 
celticianAuthor Commented:
I tried again, but the content of the mail being sent is "testfile.txt", as is, the filename in text, not the content of the file.

Ill paste the full code here

#!/bin/ksh

MAIL_FILE="testfile.txt"



rm -f $MAIL_FILE


echo "From:ScriptTol" > $MAIL_FILE
echo "To:Me Me<me@company.com>" >> $MAIL_FILE
echo "Subject:Test eMail" >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE
echo "<body style='font-family:calibri;font-size:14px;'>" >> $MAIL_FILE
echo "<h2> Test email h2 </h2>" >> $MAIL_FILE



STRING=$(cat << /var/opt/a/b/c/file_name)
STRING=${STRING#*=*- }
STRING=${STRING#*=*- }



echo ${STRING%%-*} >> $MAIL_FILE 


echo ${MAIL_FILE%%-*} |mail me@company.com

Open in new window

0
 
ozoCommented:
#!/bin/bash
MAIL_FILE="testfile.txt"
rm -f $MAIL_FILE
echo "From:ScriptTol" > $MAIL_FILE
echo "To:Me Me<me@company.com>" >> $MAIL_FILE
echo "Subject:Test eMail" >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE
echo "<body style='font-family:calibri;font-size:14px;'>" >> $MAIL_FILE
echo "<h2> Test email h2 </h2>" >> $MAIL_FILE
STRING=$(cat << /var/opt/a/b/c/file_name)
STRING=${STRING#*=*- }
STRING=${STRING#*=*- }
echo ${STRING%%-*} >> $MAIL_FILE
mail me@company.com < $MAIL_FILE
0
 
celticianAuthor Commented:
After applying the changes when executing the script i only get the next test in my email body:

Test email h2

What else can be wrong?? it seems like its ignoring the trimmed content from the file
0
 
ozoCommented:
Sorry, I don't know where the extra << came from

#!/bin/bash
MAIL_FILE="testfile.txt"
rm -f $MAIL_FILE
echo "From:ScriptTol" > $MAIL_FILE
echo "To:Me Me<me@company.com>" >> $MAIL_FILE
echo "Subject:Test eMail" >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE
echo "<body style='font-family:calibri;font-size:14px;'>" >> $MAIL_FILE
STRING=$(cat /var/opt/a/b/c/file_name)
STRING=${STRING#*=*- }
echo "<h2> Test email h2 ${STRING%%-*} </h2>" >> $MAIL_FILE
mail me@company.com < $MAIL_FILE
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now