Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Removing part of string in unix script

Posted on 2015-02-05
9
Medium Priority
?
145 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
Industry Leaders: 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If your app took Google’s lash recently, here are the 5 most likely reasons.
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

618 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