Tabbing a line a variable number of times in shell

Hi,

I had another question. I was wondering if there was a way to tab a line a variable number of times in tcsh. To go into details, I want to tab a line by how deep a file is in its path.

So here is an example code:

set filea=/blah1/blah2/blah3
set fileb=/blah1/blah2/blah3/blah4
set filec=/blah1/blah2/blah3/blah4/blah5
foreach file in (filea fileb filec)
set numtabs=echo $file | awk 'BEGIN {FS=/} ; END{print NF}'
echo "\t x $numtabs hello" #This line isn't right; is there a way to fix this?

end

So I want something like this as the output:
hello
hello

hello

I know I could do this in Perl easily, but is there a way to do this in tcsh? Appreciate any help!

#! /bin/tcsh

set filea=/blah1/blah2/blah3
set fileb=/blah1/blah2/blah3/blah4
set filec=/blah1/blah2/blah3/blah4/blah5
foreach file ($filea $fileb $filec)
    set numtabs=`echo $file | awk -F'/' '{print NF-1}'`
    set x=1
    while ( $x <= $numtabs )
        echo "\t\c"
        @ x = $x + 1
    end
    echo "hello"
end
1 Like

tcsh:

#!/bin/tcsh
set filea=/blah1/blah2/blah3
set fileb=/blah1/blah2/blah3/blah4
set filec=/blah1/blah2/blah3/blah4/blah5
foreach file ($filea $fileb $filec)
set numtabs=`echo $file | awk 'BEGIN {FS="/"} ; END{ x="";for (i=1;i<=NF;i++)x=x"\\t"; print x}'`
echo "$numtabs hello" #This line isn't right; is there a way to fix this?
end

Guru.

try this

#! /bin/bash
 
filea=/blah1/blah2/blah3
fileb=/blah1/blah2/blah3/blah4
filec=/blah1/blah2/blah3/blah4/blah5
for i in $filea $fileb $filec
do
temp=${i//\//\\v}
echo -e "$temp"
done

Hi.

For tcsh + standard utilities, TABS and SPACES:

#!/usr/bin/env tcsh

# @(#) s1	Demonstrate indents by eliminating all but "/", changing to TAB, SPACES.

# Infrastructure details, environment, commands for forum posts. 
# Uncomment setenv command to run script as external user.
# setenv PATH "/usr/local/bin:/usr/bin:/bin"
echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1 && version '=o' tcsh tr sed"
echo

set filea=/blah1/blah2/blah3
set fileb=/blah1/blah2/blah3/blah4
set filec=/blah1/blah2/blah3/blah4/blah5

# Remove everything except "/", then change "/" to a tab.
echo " Results, with tabs:"
foreach file ( $filea $fileb $filec )
  set TABS="`echo $file | tr -cd '/' | tr '/' '	'`"
  echo "${TABS}Hello"
end

# Remove everything except "/", then change "/" to spaces.
echo " Results, with spaces:"
foreach file ( $filea $fileb $filec )
  set SPACES="`echo $file | tr -cd '/' | sed 's=/=  =g'`"
  echo "${SPACES}Hello"
end

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
tcsh 6.14.00
tr (GNU coreutils) 6.10
sed GNU sed version 4.1.5

 Results, with tabs:
			Hello
				Hello
					Hello
 Results, with spaces:
      Hello
        Hello
          Hello

I prefer spaces, but tabs can be useful as well.

See man pages for details ... cheers, drl

PS: Standard advice -- use Bourne shell family for scripting.