Register
It is currently Wed Apr 23, 2014 4:20 am

Printing an array in a specific way


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Thu May 27, 2010 10:09 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi eveyrone,
I'm working a script where I read stuff from a csv file into an array, however I want to print
a new file with the array, using a string specified by the user where he tells it how he wants the fields organized.
Anyone know how to do this?

while IFS="," read -r -a array; do
numElems=`echo ${#array[*]}`
# ${array[0]}${array[1]}${array[2]}${array[3]}${array[4]}${array[5]}${array[6]}
printf "%s\n" "$str"
done < "input.txt"

Ted


Top
 Profile  
 PostPosted: Fri May 28, 2010 4:47 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You can use an easier way to get the number of elements:
Code:
numElems=${#array[*]}
I do not understand what $str is and how the user should specify the output format.


Top
 Profile  
 PostPosted: Fri May 28, 2010 8:46 am   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Well, the user would says something like this:

"$1--$4++$3-$2 $5"

Basically saying I want columns 4 and 2 swapped, columns 1 and 4 sepparated by two dashes, 4 and 3 sepparated by two '+' and so on..

Ted


Top
 Profile  
 PostPosted: Fri May 28, 2010 2:49 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
OK, then you can just use
Code:
set -- "${array[@]}"
eval echo "$format"


Top
 Profile  
 PostPosted: Sat May 29, 2010 2:24 am   

Joined: Wed Apr 14, 2010 10:23 am
Posts: 17
A more complicated and by-foot way. Needs refinement and hardening (format string insertion!). Also IMHO unusable, since this kind of stuff is nothing one wants to do with a shell. I just did it to see if it would work.

Code:
#!/bin/bash

# Testarray
array=(one two three four five)

# Testformat
format="%1%--%0%++%2%,%4%,%3%"



# Format decoder
inside_fieldspec=0
current_element_string=""
element_list=()
elements=()

for ((c=0; c<${#format}; c++)); do
  case ${format:c:1} in
    "%")
      ((inside_fieldspec = !inside_fieldspec))
      if ((!inside_fieldspec)); then
        element_list+=("$current_element_string")
        printf_format+="%s"
        current_element_string=""
      fi
    ;;
    [[:digit:]])
      if ((inside_fieldspec)); then
        current_element_string+="${format:c:1}"
      else
        printf_format+="${format:c:1}"
      fi
    ;;
    *)
      printf_format+="${format:c:1}"
    ;;
  esac
done

for index in "${element_list[@]}"; do
  elements+=("${array[index]}")
done

echo "Printf format: '$printf_format'"
echo "Element List: ${element_list[@]}"
echo "Elements: ${elements[@]}"

echo "Result:"
printf "$printf_format\n" "${elements[@]}"


Top
 Profile WWW  
 PostPosted: Sat May 29, 2010 6:07 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
What would you use then, if not a shell?

Ted.


Top
 Profile  
 PostPosted: Sun May 30, 2010 1:18 am   

Joined: Wed Apr 14, 2010 10:23 am
Posts: 17
For heavy text processing, Perl is common, for example.

Bit maybe you misunderstood, I meant the poarser code above, nothing you usually see in a shell.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP