bash and sed or awk commands to set tabs ?
I want to set more text files.
They have "tab" differently (3, 4, 6 or 5 characters space). I have to use "sed" or "awk" sette them in the same tab (for example five space characters). What is the easy way to make this . Thank you. |
Awk ought to be pretty easy, but it depends on what you are talking about. I presume the files don't actually have the tab character in them (then the spacing would be determined by the terminal settings or the program displaying them), but rather spaces to the effect of the tab positions. If, then, the items that are separated by spaces have no spaces embedded, and there are a known number of items on the line, it's an awk one liner. If the number of items on the line varies, if there are embedded spaces, if you allow quoting, if the format of lines varies (say heading lines that have no tabs), then it gets more complicated.
Describe your file formats a little more completely if you want something more specific. |
I don't think you really mean tabs----it sounds like the spacing is set with the space character.
In SED, it is trivial to match a specified # of characters, and then replace with the desired number. |
Because is not just one file , maybe awk is a good solution .
But i don't use it for now ... Yes is spaces chars , the tab is set by editors . |
If you're talking about code or markup and varied depth of identation I'd
use a tool that matches your language to do the cleanup; e.g.: perltidy, htmltidy, ... Otherwise you'll be hard-pressed to know whether the 6 spaces are a too deeply indented 4 or an 8 not indented deeply enough... If the above isn't a problem: In a "brute-force" approach you could just use sed ... Code:
sed -i.bak -r 's/(^ {4,}/ /g' * with 4 spaces) Cheers, Tink |
Would pr with the -e option help? It can remove tabs, replacing them with spaces to suit the tab positions specified.
|
mythcat, how about being a bit more talkative? ;)
How about an explicit description and sample of your files and the content and context? We're all just talking in different directions about what possibly might work if . . . without knowing exactly what your files look like. Tinkster was thinking code, where all the tabs are at the beginning for indentation of the code. I was thinking maybe a text version of a data file or spreadsheet that might have many "columns" separated by enough spaces to simulate tabs. What you actually have could be something that doesn't fit either of those. A picture (the file looks like this: several lines of sample) might be worth a lot of words. |
Quote:
Given a folder with subfolders. In python this is files (with extension. PY). I want to create a bash script which automatically receives the name of the folder and set all "tabs" at the same number of "spaces". If they manage to do and other formatting is ok. That's about it ... |
Are you saying that you want to find all *.PY files in a directory and its subdirectories and modify each one, converting the tabs to spaces?
If so then Code:
while read -r file EDIT: corrected missing closing parenthesis (red above) |
I am not sure who has the best solution yet, but I thought I might help improve the question, assuming I am on the right track.
Python code is particularly pedantic about the number of spaces/tabs that are used for indentation. Should the indentation be different by so much as a space it will not run and generate something along the lines of: Code:
IndentationError: unindent does not match any outer indentation level The problem that i forsee, if I am correct, is that no one liner will get it all completely correct, for example: Code:
for line in lines: Also from this, if the second line were say 4 spaces and the first was 8, which do you change, if any?? Problem is that maybe the second is only 4 because it relates to a previous "if" or it is at the end of the "for" loop. JM2C |
Quote:
|
Quote:
I will try with *.py files . Thank you . |
Quote:
First your solution seam to be good . But, when i try your code and i take a look , |
Quote:
Because is many folders ( each folder with one project ) and users use diff method and editors with diff tabs and if you want to work with all is crazy , because one script has 5 space tabs , another has 3 and i just use one editor with a specific number of space for tab. Do you understand ? It is very usefull script not just for my issue . |
Quote:
|
All times are GMT -5. The time now is 01:02 PM. |