Problem regarding Sed/Echo/Var Init


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem regarding Sed/Echo/Var Init
# 1  
Old 07-08-2009
Problem regarding Sed/Echo/Var Init

Greetings,

I've visited this forums for a long time and normally got an right answer but this time my problem doesn't seem to go away. What I'm trying to do is the following:
Code:
VAR="\n\nline1\nline2\nline3\nline4\nline5\nline6\nline7\n\n"

(The count of newlines is varying!)

If I echo this i get:
Code:
echo $VAR:

line1
line2
....
line7

/END

Now I want to Remove empty lines. Remember that the count of newlines is varying. I'm using the following sed command:
Code:
$VAR | sed '/^$/d'

When I echo this I get:
Code:
echo $VAR | sed '/^$/d':line1
line2
...
line7/END

However if I want to save it to $VAR something strange happens:
I use:
Code:
VAR=$(echo "$VAR" | sed '/^$/d')
echo $VAR:line1 line2 line3 line4 ... line7
echo "$VAR":line1
line2
...
line7/END

But as I'm cutting $VAR in a while loop
Code:
$VAR2=${VAR%%'\n'*}
$VAR=${VAR#*'\n'}

it doesn't seem to work. It doesn't recognize the newlines anymore and cuts away nothing. However, it cuts off in the right manner when I don't execute the sed-command, but I need to get rid of the empty newlines. I also tried changing the command to:
Code:
VAR="$(echo "$VAR" | sed '/^$/d')"
VAR="$(echo $VAR | sed '/^$/d')"

etc.
and many other versions but I can't seem to find the right answer.

Any help is highly appreciated.
Greetings,
Phil

Last edited by ph1l; 07-08-2009 at 08:24 AM..
# 2  
Old 07-08-2009
Use CODE-tags when displaying code, data or logs to enhance readability, ty.

Hm I don't really see a curiousity - maybe I missed it or did not get what you want. Here is what I did, smiliar to yours:
Code:
$> VAR="\n\nline1\nline2\nline3\nline4\nline5\nline6\nline7\n\n"
$> echo -e $VAR


line1
line2
line3
line4
line5
line6
line7


$> echo -e $VAR| grep -v ^$
line1
line2
line3
line4
line5
line6
line7
$> NEU=`echo -e $VAR| grep -v ^$`
$> echo $NEU
line1 line2 line3 line4 line5 line6 line7
$> echo "$NEU"
line1
line2
line3
line4
line5
line6
line7

I don't understand what your goal is. If the desired output is echoing the variable with double quotes around it, that's just it?! Smilie
# 3  
Old 07-08-2009
Quote:
Originally Posted by zaxxon
Use CODE-tags when displaying code, data or logs to enhance readability, ty.

Hm I don't really see a curiousity - maybe I missed it or did not get what you want. Here is what I did, smiliar to yours:
...

I don't understand what your goal is. If the desired output is echoing the variable with double quotes around it, that's just it?! Smilie
Hi zaxxon,
Thanks for trying to help me
In my script these line1,...,line7,... are the columns of different database tables, i just tried to make it simple
The problem is that I'm cutting away the different parts in a loop to get access to the single statements between the \n, e.g. i'm cutting away the back and as a result i get "line1" in the first iteration of the while loop and i cut the part out which i just selected (i cut out "line1\n" in the first iteration). I use these commands:

Code:
while [[ "$VAR" != "" ]]; do
   $VAR2=${VAR%%'\n'*}
   $VAR=${VAR#*'\n'}
done

but it doesn't seem to work with the sed-edited version. It doesn't recognize the newlines anymore and cuts away nothing. However, it cuts off in the right manner when I don't execute the sed-command and just execute the loop on the original $VAR, but I get all the empty lines of the variable and i really need to get rid of the empty newlines.

Greetings, Phil

Last edited by ph1l; 07-08-2009 at 09:46 AM..
# 4  
Old 07-08-2009
I guess I am still not getting it - let's try to make it very simple. Just post an example input and an example of the desired output. I don't understand why you want to cut out newlines that are not there. That's where I am stuck. Also is it important you save all those strings to 1 variable and then work it again with a while loop? Maybe there is a simpler more clear way if you post the requested examples Smilie Let's give it a try.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to echo a value of a var stored in another variable?

I'm writing a shell script in AIX and using ksh. I have a scenario where I have a variable A which stores $B. so when i echo "$A" it prints $B But I wish to print value stored in var b ie. \a\dir\res\ I wish to store \a\dir\res\ in a third variable C. later I want to cd into that path :... (1 Reply)
Discussion started by: simpltyansh
1 Replies

2. Shell Programming and Scripting

Problem with variable ECHO $((SED...

Hi, I'm new here so I want to say hello to everyone first! I searched google and this forum for a similar problem, but wasn't successful #! /bin/bash I'm trying to output (echo) n lines of a text file to the screen (later into another file). But I have problem with the sed command, it won't... (1 Reply)
Discussion started by: studiologe
1 Replies

3. Red Hat

init-script failing because of /etc/rc.d/init.d/functions

I encountered a problem on one of our database servers. OS: CentOS 5.5 final Kernel: 2.6.18-238.5.1.el5.028stab085.2 (OpenVZ kernel) We wrote some DB-Start/Stop-scripts ("/db2/admin/scripts_dba/start_services.ksh" and ".../stop_services.ksh") to start the database instances. (Database... (1 Reply)
Discussion started by: bakunin
1 Replies

4. Solaris

SVM Problem Regarding /var

Hi Guys! I am facing Problem with SVM I mention the problem below: I have Sunfire V210 machine and the partitions are as follows: Part Tag Flag Cylinders Size Blocks 0 root wm 0 - 6134 29.77GB (6135/0/0) 62429760 1 swap wu ... (1 Reply)
Discussion started by: sensation
1 Replies

5. UNIX for Advanced & Expert Users

Problem on init 0, execution is the same with init 6

Hi, I am experiencing a weird thing on my SUNFIRE machine with Solaris 9 OS. When I do init 0 to shutdown the machine to go to ok prompt, what it did was shutdown and reboot like an init 6 command do. I did check the corresponding rc scripts that were involved with init 0 and compared with rc... (2 Replies)
Discussion started by: Yenthanh
2 Replies

6. Linux

How to I change init levels after typing init 1

Dear all, I typed in init 1 on my redhat box as root and according to wikipedia (http://en.wikipedia.org/wiki/Runlevel): 1 Single-User Mode Does not configure network interfaces, start daemons, or allow non-root logins So now I can't connect back to it. How do I change the init back to 3?... (8 Replies)
Discussion started by: z1dane
8 Replies

7. Shell Programming and Scripting

Help with echo and sed

I am using GNU sed but this does not output "what". Why? HEY=$(echo "hey 70.70.70.70:21 what: " | sed -nr 's/.*(70.70.70.70\|71.71.71.71):(21\|22\|115\|443\|989\|990) (.*):.*/\3/p') echo $HEY (2 Replies)
Discussion started by: limmer
2 Replies

8. Linux

Problem of booting system in init 5 level

Hi, I am using RHEL4 and I am not booting my computer into graphics mode i.e. It gets hangs after the step of Enabling swap space... I.e system is not able to enter into runlevel 5. However, when I boot it into 'runlevel3' and then I do "#startx", It enetrs... (1 Reply)
Discussion started by: jagdish.machhi@
1 Replies

9. UNIX for Advanced & Expert Users

Init 6 & Init 0 problem

Hi Expert, I have encountered some problem with my SUN system. Everytime when i issue command #init 6 OR #init 0 it just logout and prompt for login again instead of rebooting the server when run init 6 and system shutdown when run init 0.. I can only reboot the system using reboot ... Was... (6 Replies)
Discussion started by: sc2005
6 Replies

10. Shell Programming and Scripting

Problem with echo for var, padded with spaces

While concatenating 2 values, one which expanded to fixed width & other not, I am not getting value expanded as fixed width. Following is script for the same : #!/bin/sh var1="abc" var2="def" var1Fxd=`echo $var1 | awk '{printf("%-6s",$0)}'` echo $var1Fxd""$var2 But, if I try - echo... (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question