The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing character ' from a variable sertansenturk Shell Programming and Scripting 3 04-19-2008 09:53 AM
Removing leading zeros from a variable toshidas2000 Shell Programming and Scripting 6 02-27-2008 01:13 PM
Newbie ? Need Help with If/Then & Line Breaks... kthatch UNIX for Dummies Questions & Answers 1 05-01-2007 08:44 PM
removing line and duplicate line ocelot UNIX for Dummies Questions & Answers 11 01-30-2007 12:44 PM
Removing first line from output Krrishv Shell Programming and Scripting 3 01-09-2007 06:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-12-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
Removing line breaks from a shell variable

Here is my snippet of code...


Code:
getDescription()
{
        DESCRIPTION=$(dbaccess dncsdb - << !  2>/dev/null|sed -e 's/hctt_description//' -e '/^$/ d'|tr -d '\r'
select hct_type.hctt_description
from hct_type,hct_profile
where hct_type.hctt_id=hct_profile.hctt_id
and hct_type.hctt_revision=hct_profile.hctt_revision
and hct_profile.hct_mac_address="${1}";
! )
        printf "  %s\n" ${DESCRIPTION:-"## No DESCRIPTION ##"}
}

Here is the data contain in that column/table...


Code:
$ dbaccess dncsdb - << !
> select hctt_description from hct_type;
> !

Database selected.




hctt_description  Explorer 2010  Rev 2.0

hctt_description  Explorer 2000 Rev 2.0

hctt_description  Explorer 2000 version 3.5

hctt_description  Explorer 2000 version 3.7

...
...

72 row(s) retrieved.

Database closed.

The problem is when I print out the "DESCRIPTION" variable in the first segment, it displays with linebreaks in each white space, as such:


Code:
  Explorer
  8300
  version
  1.2



How do I reformat the variable so the output looks like this?


Code:
  Explorer 8300 version 1.2

Thanks.
  #2 (permalink)  
Old 09-12-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

Code:
echo ${DESCRIPTION:-"## No DESCRIPTION ##"} | tr '\n' ' '

  #3 (permalink)  
Old 09-12-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
Quote:
Originally Posted by anbu23
Code:
echo ${DESCRIPTION:-"## No DESCRIPTION ##"} | tr '\n' ' '
That works.


BUT...


If I do this:


Code:
DESCRIPTION=`echo ${DESCRIPTION:-"## No DESCRIPTION ##"} | tr '\n' ' '`
printf " %s\n" ${DESCRIPTION}

I get this (actual output from my script):



Code:
00:0F:21:61:68:06
  Explorer
  8300
  version
  1.2

00:14:F8:BA:56:84
  Explorer
  8300
  version
  2.0

00:0F:21:61:6A:F6
  Explorer
  8300
  version
  1.2

00:0A:73:3C:4B:90
  ##
  No
  DESCRIPTION
  ##

Interesting.

Now I suspected that perhaps the '\n' in my "printf" statement may be the culprit. Sure enough, I made the following change:


Code:
        DESCRIPTION=`echo ${DESCRIPTION:-"## No DESCRIPTION ##"}|tr '\n' ' '`
        printf "  %s" ${DESCRIPTION}

and got the following output...


Code:
00:0F:21:61:68:06
  Explorer  8300  version  1.2
00:14:F8:BA:56:84
  Explorer  8300  version  2.0
00:0F:21:61:6A:F6
  Explorer  8300  version  1.2
00:0A:73:3C:4B:90
  ##  No  DESCRIPTION  ##

So I removed it from my original line which now looks like this and tried it. Got same output as above...


Code:
 printf "  %s" ${DESCRIPTION:-"## No DESCRIPTION ##"}

So the "final fix" is...


Code:
        printf "  %s" ${DESCRIPTION:-"## No DESCRIPTION ##"}
        printf "\n"

Which give this output:


Code:
00:0F:21:61:68:06
  Explorer  8300  version  1.2

00:14:F8:BA:56:84
  Explorer  8300  version  2.0

00:0F:21:61:6A:F6
  Explorer  8300  version  1.2

00:0A:73:3C:4B:90
  ## No DESCRIPTION ##

  #4 (permalink)  
Old 09-12-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
But there is one more thing...

My string has two whitespaces between each word instead of one. How did that happen? And more importantly, how do I remove the extra whitespace?

Thanks.
  #5 (permalink)  
Old 09-12-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
to remove extra spaces
Quote:
tr -s ' '
  #6 (permalink)  
Old 09-12-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
I discovered the "printf" statement affects how variables are displayed depending how the variable is referenced...


Code:
X="A short sentence"


Code:
print $X

A short sentence


Code:
printf "%s" $X

Ashortsentence


Code:
printf " %s" $X

 A short sentence


Code:
printf "  %s" $X

  A  short  sentence


Code:
printf "%s\n" $X

A
short
sentence


Code:
printf " %s\n" $X

 A
 short
 sentence

And finally, to get it working correctly...


Code:
printf "  %s\n" "$X"

  A short sentence

Gotta have quotes around the variable reference.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0