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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace leading spaces with single TAB.

Status
Not open for further replies.

motoslide

MIS
Oct 30, 2002
764
US
I use a utility to help modify text files, but when they are saved back to the disk the TAB's have been replaced with 4-space characters. I want to undo that.

I tried this:

# tr '^ ' '\011' <oldfile >newfile

But, this replaced the leading 4-spaces with 4TABs. Would sed be a better choice? There will be a few lines which will have 8-spaces that I'd like converted to 2-TABs.
So, what I'm looking for is a command to replace any instance of 4 spaces with a single TAB.

To help clarify, consider this example (using "_" to simulate a SPACE, and "<tab>" for TAB:

Original File:
This is line 1.
____This is line 2.
____This is line 3.
This is line 4.
________This is line 5.

Desire result:
This is line 1.
<tab>This is line 2.
<tab>This is line 3.
This is line 4.
<tab><tab>This is line 5.

Thanks folks!
 
Yes, sed would be better.

Code:
sed 's/    /	/g'
[COLOR=red]#       ^     ^
#  4 spaces  tab[/color]
 
You may also consider pr:
pr -e4 -i4 -t /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks to both ideas. The sed option gets me closer to what I need, as I was able to specify only the beginning part of the line like so:

cat file|sed 's/^ /\t/g` > newfile

I'll have to play more with the pr command. My text had too many other instances of multiple space characters which got expanded to tabs.

Each allowed me to clean up the remaining issues manually. So, I'm good for now.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top