Solved

Removing part of string in unix script

Posted on 2015-02-05
9
118 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
MAIL_FILE=`cat /var/opt/a/b/c/FILE_NAME`
MAIL_FILE=${MAIL_FILE#*=*- }
echo ${MAIL_FILE%%-*} |mail ME@domain.com
0
 

Author Comment

by:celtician
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
A short article about a problem I had getting the GPS LocationListener working.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now