More efficient way to print variable values?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers More efficient way to print variable values?
# 1  
Old 08-30-2014
More efficient way to print variable values?

Hello,

Through the process of a executing a shell script, I extract the values for a number of variables (arbitrarily declared as a through i) and towards the end I print them out like shown below:
Code:
#!bin/sh

# bits of code to get values for the variables
......
......

# print the values
printf "$a\t$b\t$c\t$d\t$e\t$f\t$g\t$h\t$i\n" >>  Values.out

exit 0

Is there a more efficient way to put the values into an array and print them out with specified delimiters?

Thank you for your input!

~Guss
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) when posting multi-line input, output, and code segments.

Last edited by Don Cragun; 08-30-2014 at 04:36 PM.. Reason: Chane ICODE tags to CODE tags; remove COLOR tags.
# 2  
Old 08-30-2014
If any of your variables could ever contain a percent sign or a backslash character, you should not put the values themselves in your format string. A much safer way would be:
Code:
printf '%s\t' "$a" "$b" "$c" "$d" "$e" "$g" "$h"
print '%s\n' "$i

or:
Code:
printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$a" "$b" "$c" "$d" "$e" "$g" "$h" "$i"

or, if you had an array containing 8 elements:
Code:
printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "${array[@]}"

If you had the variables in an array and wanted to print each value on a separate line, you could use:
Code:
printf '%s\n' "${array[@]}"

no matter how many elements are in the array.
# 3  
Old 08-31-2014
${array[@]} expands each element to a separate word. printf will implicitly loop if more arguments are given than format specifiers.

${array[*]} expands to a single word with values separated by the first character of IFS
IFS is normally $' \t\n' you can either change the order permanently or switch back and forth by saving it, use a subshell or a function and local variable, ...

subshell example
Code:
$ ( IFS=:; printf '%s\n' "${a[*]}"; ); printf '%q\n' "$IFS"
1:2:3:4:5:6:7:8:9:10
$' \t\n'

function example
Code:
$ printvalues() { local IFS=:; printf "%s\n" "$*"; }; printvalues "${a[@]}"; printf '%q\n' "$IFS"
1:2:3:4:5:6:7:8:9:10
$' \t\n'

# 4  
Old 08-31-2014
Setting IFS before read works fine because that command uses the variable..

Setting IFS before printf won't do because the shell expands the array before the command is executed.
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 08-31-2014
Quote:
Originally Posted by neutronscott
Setting IFS before read works fine because that command uses the variable..

Setting IFS before printf won't do because the shell expands the array before the command is executed.
Ouch. Yes. Sorry.
I need more sleep.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Most efficient method to extract values from text files

I have a list of files defined in a single file , one on each line.(No.of files may wary each time) eg. content of ETL_LOOKUP.dat /data/project/randomname /data/project/ramname /data/project/raname /data/project/radomname /data/project/raame /data/project/andomname size of these... (5 Replies)
Discussion started by: h0x0r21
5 Replies

2. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

How to print huge values in sqlplus variable in UNIX?

Hi, I ahve a unix code as below. sqlTxt=$* sqlReturn=`echo "set feedback off; set heading off; set term off; set verify off; set serveroutput on size unlimited set lin 1000; VARIABLE GV_return_val NUMBER; VARIABLE GV_script_error varchar2(4000); EXEC :GV_return_code := 0; EXEC... (6 Replies)
Discussion started by: bhaski2012
6 Replies

5. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

7. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

print decimal values

Hi guys I'm trying to print average of 2 columns. awk '{print ($1+$2)/2}' file.txt Its printing average but not giving decimal values its giving 3.05521e+08 instead of 305521.... I tried %f to print float values but not quiet connected Could you help plz:confused: (5 Replies)
Discussion started by: repinementer
5 Replies

10. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies
Login or Register to Ask a Question