Solved

Removing part of string in unix script

Posted on 2015-02-05
9
129 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

809 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