Change indentation in scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change indentation in scripts
# 1  
Old 03-13-2013
Change indentation in scripts

Hi,

I am a professional in writing shell scripts,
and I am using a one-space indentation like this
Code:
for i in file1 file2
do
 if [ -f "$i" ]
 then
  echo "$i"
 fi
done

so very deeply nested stuff still fits on my screen.
At release time I usually double the indentation via
Code:
sed 's/^ */&&/'

to make it better readable for others.

Now my question: how to do the reverse?
I.e. if I get a released script back for a rework, how can I get half of the indentation?
# 2  
Old 03-13-2013
Just a suggestion, how about using awk ?
Code:
awk ' {
        match($0, /^[ \t]*/)
        if ( (RSTART + RLENGTH ) > 1 )
                print substr($0, RSTART, RLENGTH / 2) substr($0, RSTART + RLENGTH)
        else
                print $0
} ' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-13-2013
Thanks for bringing the match() function to my attention.
Indeed this is the solution!
# 4  
Old 03-13-2013
Quote:
Originally Posted by MadeInGermany
so very deeply nested stuff still fits on my screen.
Yikes, if stuff is that deeply nested the programming gods are telling you it's time for some functions and/or simplification.

I worked for a software house where one of their programming rules was all functions must exit at the bottom (no returning in the middle) and a lot of their code ended up with huge nested levels, as each validation you did pushed everything else over 1 more indent.
# 5  
Old 03-13-2013
Yes, much better not to nest that much. I agree with chubler_xl advice.
# 6  
Old 03-13-2013
chubler_XL is correct. I use 5 spaces indentation and if a line doesn't fit within 80 characters i think hard if it is really good to do it that way (in most cases it isn't and it is the rare exception if it is).

To reduce the indentation in sed (replace "<b>" and "<t>" with literal blanks/tabs):

Code:
sed ':search
     /^<b><b>/ {
                 s/^\([<b>]*\)\(<b><b>\)\([^<b>]\)/\1;\3/
                 b search
              }

     :replace
     /^;/     {
                 s/^\(;*\);/\1<b>/
                 b replace
              }' /path/to/input


You can easily adapt this to other levels of indentation by modifying the parts marked bold to contain more or less blanks. Note that lines have to start leftmost - no effort is made to deal with lines not starting at character position 1.

I hope this helps.

bakunin
# 7  
Old 03-14-2013
I agree that long lines should be avoided, but there is a trivial way to cut the number of leading spaces on every line in a file in half:
Code:
sed 's/^\( *\)\1/\1/'


Last edited by Don Cragun; 03-14-2013 at 12:42 AM.. Reason: Edit tags
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Indentation for Lists in html

An writing some html instructions to have text beside an image. The image is located on the left and I want the tect to appear on the right side of the image. The text includes a list. The problem is that the list boxes and not shifted. The text within the list align on the left with the... (20 Replies)
Discussion started by: kristinu
20 Replies

2. Homework & Coursework Questions

Indentation using awk

1. The problem statement, all variables and given/known data: I need to write an awk program who does this (sorry its too big) http://i.stack.imgur.com/yzSqB.jpg 2. Relevant commands, code, scripts, algorithms: .. 3. The attempts at a solution (include all code and scripts): declaring a... (2 Replies)
Discussion started by: guy9050
2 Replies

3. UNIX for Dummies Questions & Answers

Indentation using awk

I need to write an awk program who does this http://i.stack.imgur.com/yzSqB.jpg I'd be very grateful for any help, Thanks a lot (1 Reply)
Discussion started by: guy9050
1 Replies

4. Shell Programming and Scripting

Can anybody change this into Linux shell scripts?

@echo off SET "p0=%~0" SET "p1=%~1" SET "p2=%~2" SET "p3=%~3" SET "p4=%~4" SET "p5=%~5" SET "p6=%~6" SET "p7=%~7" SET "p8=%~8" SET "p9=%~9" SHIFT SET "p10=%~9" SHIFT SET "p11=%~9" SET "zip_path=D:\OraOutput\interco\%p10%" echo Program... (5 Replies)
Discussion started by: monisha
5 Replies

5. UNIX for Advanced & Expert Users

What files or programs have the ability to change your default network scripts and config

What files or programs have the ability to change your default network scripts and config files? All 3 of these very important files got changed on their own. /etc/sysconfig/network-scripts/ifcfg-wlan0 /etc/sysconfig/networking/devices/ifcfg-wlan0... (4 Replies)
Discussion started by: cokedude
4 Replies

6. Shell Programming and Scripting

Indentation help

Hi I have coded 300 line script.Its not indented properly.i am not good at indentation. I would appreciate your help on this. i want to use a 4 space indentataion.Hence if i "set tabstop=4" and use tabs for coding and if some one else open this script in their system it looks unindented since... (11 Replies)
Discussion started by: pinnacle
11 Replies

7. Shell Programming and Scripting

Scripts fails if you change its code during the execution.

So a script is working properly (tested many times) , then you add a new fine piece of code ,finaly its fails generally with a syntax error at the last line of the script. :confused:... does anybody why this happens? >uname -a HP-UX test... (4 Replies)
Discussion started by: Klashxx
4 Replies

8. Shell Programming and Scripting

How to make the same change in multiple shell scripts?

I would like to make the same change in multiple shell script files and would like to know if anyone can be of some help? I would appreciate it. (4 Replies)
Discussion started by: rdakhan
4 Replies

9. Programming

A question of indentation

Hi, Using GNU indent(1) I tried to indent a C source file which has no indentation (all lines start at column 1). The result I am trying to achieve, should look like this with the exception that only tabs are used for indentation (no spaces). Unfortunately, I couldn't find the appropriate... (1 Reply)
Discussion started by: Michael Iatrou
1 Replies

10. Shell Programming and Scripting

find all scripts to change DNS name??

hi, we are going for a new unix box and the ip and DNS name has to be changed in all the scripts, where ever it is hard coded. i was trying the below mentioned command to list all such scripts where the ip/dns name is hard coded: find / -type f -print | xargs grep -l "ip address" >>... (0 Replies)
Discussion started by: Bhups
0 Replies
Login or Register to Ask a Question