![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
Quote:
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 ## |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|