Tabbing a line a variable number of times in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tabbing a line a variable number of times in shell
# 1  
Old 02-28-2012
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:

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!
# 2  
Old 02-28-2012
Code:
#! /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

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-28-2012
tcsh:


Code:
#!/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.
# 4  
Old 02-28-2012
try this

Code:
#! /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

# 5  
Old 03-01-2012
Hi.

For tcsh + standard utilities, TABS and SPACES:
Code:
#!/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:
Code:
% ./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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the line number in shell script

I have one text file 1 2 3 a 5 4 4 3 where i want to print the line number while read line do line_no=`awk '{print NR, $0}'` echo 'In line no $line_no' done <$txt_file If i run the above code, it will print 'In line no 1 1 2 3' It prints the line number with the whole... (3 Replies)
Discussion started by: RJG
3 Replies

2. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

3. Shell Programming and Scripting

How to print a particular character n number of times in a line??

hi, Is it possible to print a particular character n number of times in a line? for example. i am print the following line using echo command.. echo "files successfully moved" i want to count the number of characters that are been displayed. i am doin it using echo "files... (8 Replies)
Discussion started by: Little
8 Replies

4. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

5. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

6. Shell Programming and Scripting

How to assign a shell variable to a NUMBER parameter in pl/sql?

I have the below script running for generating file from PL/SQL stored procedure. I need to declare a shell variable and then pass this to sqlplus command to pass the same as a INPUT parameter for the stored procedure. Please help to do this. #!/bin/sh minlimit=0 maxlimit=10 size=100 while... (0 Replies)
Discussion started by: vel4ever
0 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies

9. Shell Programming and Scripting

Capturing a number at the end of line and store it as variable

Hello, Would someone guide me on how to write a shell script the would search for a phone no using at the end text file using sed or awk and store it in a varaible or print it. The text file is in this form text or numbers in first line text or numbers in second line . . . Firsname... (6 Replies)
Discussion started by: amuthiga
6 Replies

10. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies
Login or Register to Ask a Question