1

I have a file called temp1

   sCellEventTrap-03-27 TRAP-TYPE  -- CAC Code: 01
        ENTERPRISE compaq
        VARIABLES  { scellNameDateTime,
                     scellSWComponent,
                     scellECode,
                     scellCAC,
                     scellEIP}
        DESCRIPTION
             "Severity: Critical -- failure or failure imminent. A physical disk drive has experienced an ID block inconsistency."
           --#TYPE      "StorageCell Event"
           --#SUMMARY   "SCellName-TimeDate %s : SWCID %d : ECode: %d : CAC %d : EIP %d."
           --#ARGUMENTS {0,1,2,3,4,}
           --#SEVERITY  CRITICAL
           --#TIMEINDEX 136
           --#STATE     WARNING
        ::= 13600807

Applying this on temp1

prvar=$(awk '/VARIABLES/,/DESCRIPTION/ {gsub("VARIABLES"," "); gsub("DESCRIPTION"," "); gsub("}"," "); gsub("{"," ");gsub(","," ");print;print ":%s"}' temp1)

Gives me (what I want)

echo $prvar

scellNameDateTime :%s scellSWComponent :%s scellECode :%s scellCAC :%s scellEIP :%s :%s

However this (the format not wanted)

echo "$prvar" 

             scellNameDateTime 
:%s
                     scellSWComponent 
:%s
                     scellECode 
:%s
                     scellCAC 
:%s
                     scellEIP 
:%s

:%s

I am to use prvar in echo along with text like this

echo "blah blah blah $prvar"

Doing this what I get

blah blah blah              scellNameDateTime 
:%s
                     scellSWComponent 
:%s
                     scellECode 
:%s
                     scellCAC 
:%s
                     scellEIP 
:%s

:%s

What I want it to print


blah blah blah scellNameDateTime :%s scellSWComponent :%s scellECode :%s scellCAC :%s scellEIP :%s :%s

The "" on the prvar when using echo create the problem. What can I do so that prvar is printed in a just above format?

EDIT
Well turns out it doesnt make a difference whatever I type in my SCRIPT echo "$prvar" and echo $prvar print the same thing --- the format i don't want. The Terminal and the script are behaving differently. Any work around for this?

1

You can use parameter expansion and an extended glob to "normalize" the whitespace.

# enable extended globs so that +([[:space:]]) matches one or more whitespace
shopt -s extglob

# replace all occurances of one or more whitespace with a single space
prvar=${prvar//+([[:space:]])/ }

echo "blah blah blah $prvar blah blah"

This allows you to safely quote the variable. If you don't quote the variable, word splitting is not the only thing that will happen. The shell will also attempt to replace words with filenames, if they contain glob characters like *, ?, [...].

0

You dont have to quote (") everything in echo. You can have many strings. Simply dont quote prvar:

echo "blah blah blah" $prvar

In the shell (which is expanding $prvar for you before echo even sees it), spaces inside quotes (") are preserved, but spaces and tabs outside quotes are reduced to one space. eg

echo "a        b"        d            e
a        b d e
  • What if it were like this "blah blah blah $prvar blah blah blah" ? then what can I do ? – SamFlynn Jun 28 '15 at 7:30
  • 2
    @SamFlynn: echo "blah blah blah" $prvar "blah blah blah" – Cyrus Jun 28 '15 at 7:36
  • Well turns out it doesnt make a difference whatever i type in my script echo "$prvar" and echo $prvar print the same thing --- the format i don't want. The Terminal and the script are behaving differently any work around for this? – SamFlynn Jun 28 '15 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.