Solved

Removing part of string in unix script

Posted on 2015-02-05
9
141 Views
Last Modified: 2015-03-05
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
Comment
Question by:celtician
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40591658
#!/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
 

Author Comment

by:celtician
ID: 40592169
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
 
LVL 84

Expert Comment

by:ozo
ID: 40592244
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!

 

Author Comment

by:celtician
ID: 40592352
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
 
LVL 84

Expert Comment

by:ozo
ID: 40592367
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
 

Author Comment

by:celtician
ID: 40594637
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
 
LVL 84

Expert Comment

by:ozo
ID: 40594654
#!/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
 

Author Comment

by:celtician
ID: 40594759
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
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40594764
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question