How to append values to a string?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to append values to a string?
# 1  
Old 04-19-2013
How to append values to a string?

Hi,

Requesting some help with a problem I am facing with string function in UNIX. I wish to create 2 string variables: 1st header string containing output_1, output_2, .. , output_<n> and 2nd data string containing the filename separated by colon (":") and corresponding filesize separated by comma as shown below
E.g.
Code:
output_1,output_2,output_3,
file1:303,file2:304:file3:443

I have written the following piece of code to perform the same, but I am not getting the values of the variables appended to my string variables header_str and output_str? Can you please advise how can i get the two string variables as shown below:
Code:
header_str=output_1,output_2,output_3,
output_str=file1:303,file2:304:file3:443,

Code:
        set -x;
        typeset -L header_str; typeset -L output_str;
        typeset -i op_cnt=1;
        ls -lrt <dirname>|awk '{FS = ",|[ \t]+"} NR>1 { print $9, $5 }'|tr " " ":"|while read line
        do
          header_str=$(echo $header_str,output_file$op_cnt)
          output_str=$(echo $output_str,$line)
          op_cnt=`expr $op_cnt + 1`
        done

        echo  header_str is $header_str;
        echo output_str is $output_str;


Last edited by Scrutinizer; 04-19-2013 at 04:56 PM.. Reason: extra code tags
# 2  
Old 04-19-2013
$ ls -l
total 16
Code:
$ ls -l
-rwxr-xr-x 1 372 Apr 19 11:43 test.sh
-rw-rw-r-- 1  58 Apr 19 11:44 xxx
-rw-rw-r-- 1  87 Apr 19 11:44 yyy
-rw-rw-r-- 1  29 Apr 19 11:33 zzz

Code:
$ cat test.sh
ls -rt > /tmp/list_of_files
number_of_files=`ls | wc -l`
header_str="header_str="
output_str="output_str="
n=1
while read file_name; do
  file_size=`cat $file_name | wc -c`
  header_str="${header_str}output_$n,"
  output_str="${output_str}$file_name:$file_size,"
  n=`expr $n + 1`
done < /tmp/list_of_files

echo header_str is $header_str;
echo output_str is $output_str;

Code:
$ ./test.sh
header_str is header_str=output_1,output_2,output_3,output_4,
output_str is output_str=zzz:29,test.sh:372,xxx:58,yyy:87,

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-19-2013
Your problem is explained quite easily and quite often in these forums: piped commands are executed in a subshell, variables of which cannot get handed back to the calling shell. You can use temp files as hanson44 showed, you could use sth. like process substitution (should your shell allow for that), or you could try to print your results to stdout so they can be assigned to shell variables by command substitution. Tr ysth. like
Code:
ls -l |
  awk '{FS = "[, \t]+"} NR>1 { print $9, $5 }'|
  tr " " ":"|
  { while read line
      do header_str+=,output_file$op_cnt
         output_str+=,$line
         ((op_cnt++))
      done
   echo header_str is $header_str
   echo output_str is $output_str   
  }

and use command substitution $(...) for the var assignment.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-19-2013
Thank you for all your replies. As advised by RudiC, when I defined the string variables header_str and output_str as global variable I was very well able to resolve them outside of the piped commands.

Many thanks for your invaluable support and guidance.

Code:
set -x;
        export header_str; export output_str;
        typeset -i op_cnt=1;
        ls -lrt <dirname>|awk '{FS = ",|[ \t]+"} NR>1 { print $9, $5 }'|tr " " ":"|while read line
        do
          header_str=$(echo $header_stroutput_file$op_cnt,)
          output_str=$(echo $output_str$line,)
          op_cnt=`expr $op_cnt + 1`
        done

        echo  header_str is $header_str;
        echo output_str is $output_str;


Last edited by vkumbhakarna; 04-19-2013 at 05:43 PM..
# 5  
Old 04-19-2013
Thank you for clearly posting an interesting problem.
# 6  
Old 04-20-2013
Quote:
Originally Posted by RudiC
Your problem is explained quite easily and quite often in these forums: piped commands are executed in a subshell, variables of which cannot get handed back to the calling shell.
This is correct, but only for the bash shell. In a true ksh (that is: not "pdksh" and similar clones) the variable scope is as thread-o/p obviously expected it to be.

This is one of the reasons i prefer ksh over bash.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 04-21-2013
I inferred from his code snippet that it was more ksh than bash. But, then, why are the variables in his example empty, assuming he is using ksh (which, btw, we don't know for sure)?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

<< generate alphabets and append in the input values >>

Hi Team, Pls help to get the desired output. I have a input like below nodecount=10 host=na7-db1-1-chi nodecount can be 10 or 8 based on this we need a output (in single line) like below na7-db1-1-chi:A na7-db1-2-chi:B na7-db1-3-chi:C na7-db1-4-chi:D na7-db1-5-chi:E... (4 Replies)
Discussion started by: kamauv234
4 Replies

2. Shell Programming and Scripting

Append values of duplicate entries

My input file is: LOC_Os01g01870 GO:0006139 LOC_Os01g01870 GO:0009058 LOC_Os01g02570 GO:0006464 LOC_Os01g02570 GO:0009987 LOC_Os01g02570 GO:0008152 LOC_Os01g04380 GO:0006950 LOC_Os01g04380 GO:0009628 I want to append the duplicate values in a tab/space... (2 Replies)
Discussion started by: Sanchari
2 Replies

3. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

4. Shell Programming and Scripting

Append a searched string with another string using sed

Hi, I need to replace and append a string in a text if grep is true. For eg: grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ? How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be, lx123 web222 xyz Want to... (8 Replies)
Discussion started by: vchee
8 Replies

5. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

6. AIX

How to append spaces to string values?

i/o file: abc,efg,xyz Required o/p file: "abc (Value + blank spaces=16) " ,"efg (Value +blank spaces=15) " ,"xyz (Value+ blank spaces =20) " In short input file value stores in result file with " i/p Value " added with spaces and are of fixed size like 16,15,20 How to do using... (2 Replies)
Discussion started by: AhmedLakadkutta
2 Replies

7. Shell Programming and Scripting

Need help with shell, trying to append or separate values in a string

Ok. I for the life of me cant figure out how to do this. I need Help. So here is what I'm trying to do. I have a block of text. They are FIPS codes for counties. Below is the block. There are probably a few ways to do this. The first line starting with ARC021....... this line is a list of... (2 Replies)
Discussion started by: chagan02
2 Replies

8. Shell Programming and Scripting

Append values before a string

hi all, i have variables a and b with values, like a="/var/tmp/new.sh /var/tmp/new2.sh" b="/TEST" how i need to append the value "/TEST" before the values for the variable "a" so that i get the output as /TEST/var/tmp/new.sh /TEST/var/tmp/new2.sh plz help me Regards, NG (2 Replies)
Discussion started by: Nandagopal
2 Replies

9. UNIX for Dummies Questions & Answers

use awk to append values

Hi, I have a file like this: tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 and what i want is: value1 value2 value3 value1 value2 value3 (15 Replies)
Discussion started by: nickrick
15 Replies

10. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies
Login or Register to Ask a Question