Removing line breaks from a shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing line breaks from a shell variable
# 1  
Old 09-12-2006
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

Smilie

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

Code:
  Explorer 8300 version 1.2

Thanks.
# 2  
Old 09-12-2006
Code:
echo ${DESCRIPTION:-"## No DESCRIPTION ##"} | tr '\n' ' '

# 3  
Old 09-12-2006
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  
Old 09-12-2006
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  
Old 09-12-2006
to remove extra spaces
Quote:
tr -s ' '
# 6  
Old 09-12-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing line breaks inside a field

Hi all, I have a csv input file with total 60 fields and the fields are not enclosed with double quotes.One of the field(50th field) in this file has line breaks in it which results in the row getting split into multiple lines.This is causing my load(to table) to fail.I tried to enforce double... (3 Replies)
Discussion started by: Bobby_2000
3 Replies

2. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

3. HP-UX

After using @, line breaks for a particular user in shell

Dear Concern, When we using @ sign, line breaks for a particular user in shell. Please advise how to resolve from the problem in HP UX. tabs@tabsdb02:/ccbs/users/tabs$ cat /etc/passwd|grep tabs tabs:RdCgOsmKee7Ps:221:201::/ccbs/users/tabs:/usr/bin/ksh... (3 Replies)
Discussion started by: makauser
3 Replies

4. UNIX for Dummies Questions & Answers

Page breaks and line breaks

Hi All, Need an urgent solution to an issue . We have created a ksh file or shell script which generates 1 DAT file. the DAT file contains extract of a select statement . Now the issue is , when we are executing the ksh file , the output is coimng with page breaks and line breaks . We have... (4 Replies)
Discussion started by: Ayaskant
4 Replies

5. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

6. Shell Programming and Scripting

Questions on removing unexpected line breaks

I am a newbie in Linux and I am having trouble with a piece of data on hand. The source data is like a|b|c|d e|f|g |h i|j|k|l m|n|o |p 1|2|3|4 5|6|7| 8 a|b|c|d e|f|g|h For each line, there should be 4 fields separated by the "|", but unfortunately there are unexpected line breaks... (13 Replies)
Discussion started by: Nekki Basara
13 Replies

7. Shell Programming and Scripting

Remove line breaks in csv file using shell script

Hi All, I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double... (4 Replies)
Discussion started by: rajak.net
4 Replies

8. Shell Programming and Scripting

Help with wc and line breaks

Hi everyone, I have gone through the forum trying to find an answer to this question but was unsuccessful. I am hoping that someone can help me with this please. I am trying to get my script to recognise line breaks from a file and to give me a result for wc of each line. So basically, if you... (7 Replies)
Discussion started by: stargazerr
7 Replies

9. UNIX for Dummies Questions & Answers

removing new line from the shell variable value

I have a small doubt that, how to chomp the new line from the bash shell variable value. In perl, i will do chomp, but how to do it in bash -- shell programming. When i redirect the date output i have a new line in it, which i would want to remove, how to do ?! (5 Replies)
Discussion started by: thegeek
5 Replies

10. Shell Programming and Scripting

shell replacing space with line breaks

I'm parsing through a large lslpp -Lc output file with a for loop. The file contains many lines similar to this: zip:zip-2.3-3:2.3: : :C:R:A file compression and packaging utility compatible with PKZIP.: :/bin/rpm -e zip: : : : :0: There appears to be no specialized or secret markup in the... (5 Replies)
Discussion started by: mshulman1980
5 Replies
Login or Register to Ask a Question