Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete blank lines from a file and indent lines. 1

Status
Not open for further replies.

skuthe

Programmer
Sep 10, 2002
33
0
0
US
Hi,
I am stuck on a problem of deleting blank lines from a file and also shifting some lines in the file to the left.

My file is something like below...
-----------------------------------
<blank>
<blank>
<blank>
<blank>
<blank>
<blank>
<blank>
/users/cdc/importfg.com
/users/ciim/ap/com/apg
/users/ciim/ap/com/apglauto.com
/users/ciim/monthend/partsub.com
/users/ciim/monthend/scrpacnt.com
/users/ciim/mrp/com/mrp.com
/users/ciim/mrp/com/mrp.com
/users/ciim/ms/com/ms.com
/users/ciim/ms/com/ms.com
/users/ciim/ms/com/ms.com
/users/ciim/ms/com/ms.com
/users/ciim/ms/com/ms.com

I need to delete the blank lines from the top and move the lines towards left so that the file is uniquely formatted.

PL NOTE that I tried the following sed command to get rid of the blank lines but IT DOES NOT WORK !!.

sed '/./,/^$/!d'

Your help is greatly appreciated.

Thanks
 
NB No awk answers here!

To remove empty lines :
sed '/^$/d' filename > newfilename
To remove line(s) with just a space :
sed '/^ /d' filename > newfilename
To remove line(s) starting with 4 spaces :
sed '/^ /d' filename > newfilename


Dickie Bird (:)-)))
 
and ......
To remove leading space :
sed 's/^ //' filename > newfilename
To remove leading 4 spaces :
sed 's/^ //' filename > newfilename


Dickie Bird (:)-)))
 
NO buddy, none of your suggestion work, I am sorry.
Actually I have finised trying all SED commands to get rid of the <blank> from file but no w/o any result. Moreover the blanks at the starting of the file are not normal blanks !! by this I mean, when I open the file in vi on HP unix box and try to move my cursor over the line the cursor does not move on the blank lines but if I tab then it moves forward by 1 tab and then goes on to next line.
I tried SED commands to get rid of the tabs as well but still no results. Don't know how to solve this ??

I am open to any and all suggestions. PL. help.
 
sed -e '/./!d' -e &quot;s/^[ $(echo -e '\t')]*//g&quot; fil.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,
You made me glad !!.
Thank you so much buddy. I have to run the script twice to get my file cleaned up.But thats better than not getting the file cleaned up at all.

Thanks again.
 
twice? why is that? It worked fine on your sample file here, bud! [grin]


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I do not know why. But when I run your script once it shifts the lines to left but does not delete the blank lines. When I run it for the second time it delete the blank lines. Secondly there is another problem, your script strips the first '/' from all the lines which screws my uniq command another that is generated by merging this cleaned file with some other file.

Pl. help.
 
ooops, sorry about that.

try that one [one line - no wrapping]:
sed -e '/^[ $(echo -e '\t')]*$/d' -e &quot;s/^[ $(echo -e '\t')]*//g&quot; file.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
No effect. Same output. The o/p is given below...
The '/' before the users is stripped... f.eg.The first line should actually look like /users/ciim/oe/com/load830.com similarly the rest of the file.

users/ciim/oe/com/load830.com
users/ciim/oe/com/ordcmpr.com
users/ciim/oe/com/loadmrel.com




users/ciim/inv/com/cumlead.com
users/ciim/mrp/com/mrp.com

users/ciim/oe/com/load830.com
users/ciim/oe/com/loadmrel.com
users/ciim/ms/com/ms.com

users/ciim/oe/com/salesovw.com
users/ciim/pay/com/salinit.com

Thanks.
 
ok, how about:
sed -e &quot;/^[ $(echo -e '\t')]*$/d&quot; -e &quot;s/^[ $(echo -e '\t')]*//g&quot;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
YES, it works !. Thank you for your persistant efforts. I really appreciate it.
Also,could you please explain the above command so that I remember it from next time.?

Thanks.
 
sed -e &quot;/^[ $(echo -e '\t')]*$/d&quot; -e &quot;s/^[ $(echo -e '\t')]*//g&quot;

-e &quot;/^[ $(echo -e '\t')]*$/d&quot;
delete any line that contain a 'space' OR tab repeted 0 or more time. '^' - stand for the beginning of a line. '$' - stand for the end of the line. The shell command '$(echo -e '\t')' - outputs a 'tab' character.

-e &quot;s/^[ $(echo -e '\t')]*//g&quot;
remove any leading space OR 'tab' character for a line. '^' - stands for the beginning of a line. We substitute any leading 'space' character with 'nothing' and we do it 'globally' (g) for ALL the lines in a file.

hth





vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 

I hesitate to suggest even a minor correction to Vlad's excellent posts but the g in the second sed command means substitute every occurence of the pattern on the line. Since it is only making one substitution per line (at the beginning), it is not necessary. The s command defaults to operating on every line in the file when it is not preceded by an address range.

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
I stand corrected - thanks, CaKiwi!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks guys, thanks for all your help. I really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top