0

I have done this way to read text file by using help from these : Split string into an array in Bash and How do I split a string on a delimiter in Bash? and trying to split string but i am getting only 1st line with this code: (i have pasted the same code in "file" which is i am reading.)

#!/bin/bash
i=0

for file in $(ls -l)
do

   if [ -f $file ] #checking for regular file
   then 
    if [ $file ==  Q1 ] || [ $file == Q1~ ] 
    then    
            continue
    else
        if [ -f $file ]
            then
               echo $file
               v=$(<$file)
          # echo ${v[0]} # prints all
#------------------ split string-----------------------
           OIFS="$IFS"
           IFS='' # split by spaces
            read -a array <<< "${v}"
        IFS="$OIFS"
           echo ${array[0]} #
           #printf "\n"
          # sed -i 's/$1/$2/g' "$file"
        else
        echo "Error: Not found"
        fi
    #i=$i+1
    #echo $i 
    fi

   fi


done
exit 0

but when using with loops, then there is no splitting (2nd code)

#!/bin/bash
i=0

for file in $(ls -l)
do

   if [ -f $file ] #checking for regular file
   then 
    if [ $file ==  Q1 ] || [ $file == Q1~ ] 
    then    
            continue
    else
        if [ -f $file ]
            then
               echo $file
               v=$(<$file)
          # echo ${v[0]} # prints all
#------------------ split string-----------------------
           OIFS="$IFS"
           while IFS=''  read -a array;
            do
           for i in "${array[@]}";do

               echo ${array[1]} #
           done
        done <<< "$v"
           #printf "\n"
          # sed -i 's/$1/$2/g' "$file"
        else
        echo "Error: Not found"
        fi
    #i=$i+1
    #echo $i 
    fi

   fi


done
exit 0
Community
  • 1
  • 1
dell pk
  • 155
  • 1
  • 6
  • `v` is not an array, so `${v[0]}` is equivalent to `$v`. As to the rest, it's not clear what you are trying to do, but please read http://mywiki.wooledge.org/BashFAQ/001 concerning how to read a file line by line before editing your question. – chepner Apr 21 '15 at 21:06
  • wt i am trying to do is 1st i am reading the txt files and then putting into array by splitting those txt in file. – dell pk Apr 21 '15 at 21:13
  • trying to understand – dell pk Apr 21 '15 at 21:25
  • 2
    But what is the point of reading the entire file into memory? If you're just going to iterate over the array elements one at a time, it would be simpler to read the file one line at a time as well. (If you're worried about performance, you're using the wrong language to start with.) – chepner Apr 21 '15 at 22:37
  • after this i want to search string ( i am not allowed to use recursive ) thats why i am saying. – dell pk Apr 22 '15 at 04:59
  • echo ${array[1]}, not something with $i ? – Walter A Apr 22 '15 at 07:24
  • 1
    I agree with @chepner, searching/replacing things can be done without an array. Perhaps explain what your goal is. I read the comment `sed -i 's/$1/$2/g' "$file"`, is that what you are trying? Than replace that line with `sed -i 's/'"$1"'/'"$2"'/g' "$file"` (interrupt the single quoted sed string for vars in double quotes). – Walter A Apr 22 '15 at 07:28
  • thanks dude a better help i got. :) – dell pk Apr 22 '15 at 14:19

0 Answers0