Use loop var i within Cut Command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use loop var i within Cut Command
# 1  
Old 07-09-2009
Use loop var i within Cut Command

Hi,

In the following bash code rather than cutting at a predefined character I would like to cut at position i (i var from loop).
Is this possible? I have tried eval, but either it's not possible or my syntax is wrong.

thanks

Nick

Code:
for i in {1..9}
do
       
        theChar=$(echo "$permissions" | cut -c2)
        checkPermissions $theChar
        charArray[i]=$response
done

# 2  
Old 07-09-2009
This seems to work:
Code:
permissions="rwxr-x---"
for i in {1..9}
do
  echo i = $i       
  theChar=$(echo "$permissions" | cut -c$i)
  echo theChar = $theChar
done

Code:
$ bash ./arraytest.sh
i = 1
theChar = r
i = 2
theChar = w
i = 3
theChar = x
i = 4
theChar = r
i = 5
theChar = -
i = 6
theChar = x
i = 7
theChar = -
i = 8
theChar = -
i = 9
theChar = -
$

Also I presume that:
charArray[i]
should be
charArray[$i]
?

Not that I have got any of that part of the code working..

http://unstableme.blogspot.com/2008/...sh-script.html looks helpful on using arrays in shells scripting.
# 3  
Old 07-09-2009
Hi,

Thanks for your prompt reply.
You are right. should be charArray[$i]

cut -c$i seems obvious now. But I couldn't see it for the life of me at the time.

thanks again.

/N
# 4  
Old 07-10-2009
Some more generic solution for this kind of needs. Works every Posix compatible shells (ksh, bash, ...).
Code:
# add space after every char (char between space and z)
perm=$( echo "$permissions" | sed "s/[ -z]/& /g"  )
# after that this is easy
i=1
for c in $perm
do
       charArray[$i]=$c
       (( i+=1 ))
done
echo "${#charArray[*]}"
echo "${charArray[*]}"

Or using builtin substr command, not external ex. cut, awk, expr, ...
Code:
i=0
len=${#permissions}
while (( i< len ))
do
       charArray[$i]=${permissions:$i:1}
       (( i+=1 ))
done
echo "${#charArray[*]}"
echo "${charArray[*]}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut command not working in for loop

grep -Fxvf testdata.xls file_GTDA1.xls >file_GTDA.xls SLS_COUNT=`grep 'GTDA_Dly_Sls' file_GTDA.xls |wc -l` PMIX_COUNT=`grep 'GTDA_Dly_Pmix' file_GTDA.xls |wc -l` if ; then var1=`cat file_GTDA.xls|grep 'GTDA_Dly_Sls_'` var4="|" for i in $var1... (7 Replies)
Discussion started by: renuk
7 Replies

4. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

5. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

6. Shell Programming and Scripting

Using a loop with cut

I don't know if I described this right, but I am new to scripting and this is giving me a little bit of trouble, but I will explain what I am trying to do. Each time this is run, I want it to grab and save ls -l /home to data.txt. ls -l /home > data.txt Now the part I am getting confused... (4 Replies)
Discussion started by: ninjafish
4 Replies

7. Shell Programming and Scripting

Problem in getting data from a loop using grep and cut

The script is following : for each_rec in <file_name> do count=`cut -c -2 ${each_rec} | grep "45"` echo ${count} if ] then amount=`cut -c 24-35 ${each_rec}` echo ${amount} else echo "failed" fi done And the file looks like below : ... (4 Replies)
Discussion started by: mady135
4 Replies

8. UNIX for Dummies Questions & Answers

Cut not working in a loop

I have a function "MyPrint" that runs great on a file (BaseData.txt) that has one line of data. If i add rows to the text file it's reading the tFile variable becomes a list of every field 2 in the file. To correct this, i tried to call the function from a loop where i read one line at a time and... (4 Replies)
Discussion started by: KME
4 Replies

9. Shell Programming and Scripting

while loop inc a var help please

OK guys I think this is an easy one but i am having a bit of trouble getting a while loop I have to do what I want. I need it to increment a number 1 - 99 but I need it to use 2 digits. So not 1,2,3,4,5...99 but 01,02,03,04,05,06,07...10,11,12,...99. Always using 2 digits. I tried this: ... (3 Replies)
Discussion started by: NewSolarisAdmin
3 Replies

10. Shell Programming and Scripting

Storing space delimited line in var with loop?

I have a script that converts a file into an html table. This script works fine for a 1 column table. However, I'm trying to do this for a multi-column table. My input file will look something like this: a b c d e f g h i My script basically works by taking in each line and putting that... (2 Replies)
Discussion started by: eltinator
2 Replies
Login or Register to Ask a Question