Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to append values to a string? Post 302796437 by vkumbhakarna on Friday 19th of April 2013 02:17:12 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
The talloc string functions.(3) 				      talloc					   The talloc string functions.(3)

NAME
The talloc string functions. - talloc string allocation and manipulation functions. Modules The talloc debugging support functions To aid memory debugging, talloc contains routines to inspect the currently allocated memory hierarchy. Functions char * talloc_strdup (const void *t, const char *p) Duplicate a string into a talloc chunk. char * talloc_strdup_append (char *s, const char *a) Append a string to given string. char * talloc_strdup_append_buffer (char *s, const char *a) Append a string to a given buffer. char * talloc_strndup (const void *t, const char *p, size_t n) Duplicate a length-limited string into a talloc chunk. char * talloc_strndup_append (char *s, const char *a, size_t n) Append at most n characters of a string to given string. char * talloc_strndup_append_buffer (char *s, const char *a, size_t n) Append at most n characters of a string to given buffer. char * talloc_vasprintf (const void *t, const char *fmt, va_list ap) Format a string given a va_list. char * talloc_vasprintf_append (char *s, const char *fmt, va_list ap) Format a string given a va_list and append it to the given destination string. char * talloc_vasprintf_append_buffer (char *s, const char *fmt, va_list ap) Format a string given a va_list and append it to the given destination buffer. char * talloc_asprintf (const void *t, const char *fmt,...) Format a string. char * talloc_asprintf_append (char *s, const char *fmt,...) Append a formatted string to another string. char * talloc_asprintf_append_buffer (char *s, const char *fmt,...) Append a formatted string to another string. Detailed Description talloc string allocation and manipulation functions. Function Documentation char* talloc_asprintf (const void *t, const char *fmt, ...) Format a string. This function is the talloc equivalent of the C library function asprintf(3). This functions sets the name of the new pointer to the new string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * Parameters: t The talloc context to hang the result off. fmt The format string. ... The parameters used to fill fmt. Returns: The formatted string, NULL on error. char* talloc_asprintf_append (char *s, const char *fmt, ...) Append a formatted string to another string. This function appends the given formatted string to the given string. Use this variant when the string in the current talloc buffer may have been truncated in length. This functions sets the name of the new pointer to the new string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * If s == NULL then new context is created. Parameters: s The string to append to. fmt The format string. ... The parameters used to fill fmt. Returns: The formatted string, NULL on error. char* talloc_asprintf_append_buffer (char *s, const char *fmt, ...) Append a formatted string to another string. This is a more efficient version of talloc_asprintf_append(). It determines the length of the destination string by the size of the talloc context. Use this very carefully as it produces a different result than talloc_asprintf_append() when a zero character is in the middle of the destination string. * char *str_a = talloc_strdup(NULL, "hello world"); * char *str_b = talloc_strdup(NULL, "hello world"); * str_a[5] = str_b[5] = ' ' * * char *app = talloc_asprintf_append(str_a, "%s", ", hello"); * char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello"); * * printf("%s0, app); // hello, hello (app = "hello, hello") * printf("%s0, buf); // hello (buf = "hello world, hello") * If s == NULL then new context is created. Parameters: s The string to append to fmt The format string. ... The parameters used to fill fmt. Returns: The formatted string, NULL on error. See Also: talloc_asprintf() talloc_asprintf_append() char* talloc_strdup (const void *t, const char *p) Duplicate a string into a talloc chunk. This function is equivalent to: * ptr = talloc_size(ctx, strlen(p)+1); * if (ptr) memcpy(ptr, p, strlen(p)+1); * This functions sets the name of the new pointer to the passed string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * Parameters: t The talloc context to hang the result off. p The string you want to duplicate. Returns: The duplicated string, NULL on error. char* talloc_strdup_append (char *s, const char *a) Append a string to given string. The destination string is reallocated to take strlen(s) + strlen(a) + 1 characters. This functions sets the name of the new pointer to the new string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * If s == NULL then new context is created. Parameters: s The destination to append to. a The string you want to append. Returns: The concatenated strings, NULL on error. See Also: talloc_strdup() talloc_strdup_append_buffer() char* talloc_strdup_append_buffer (char *s, const char *a) Append a string to a given buffer. This is a more efficient version of talloc_strdup_append(). It determines the length of the destination string by the size of the talloc context. Use this very carefully as it produces a different result than talloc_strdup_append() when a zero character is in the middle of the destination string. * char *str_a = talloc_strdup(NULL, "hello world"); * char *str_b = talloc_strdup(NULL, "hello world"); * str_a[5] = str_b[5] = ' ' * * char *app = talloc_strdup_append(str_a, ", hello"); * char *buf = talloc_strdup_append_buffer(str_b, ", hello"); * * printf("%s0, app); // hello, hello (app = "hello, hello") * printf("%s0, buf); // hello (buf = "hello world, hello") * If s == NULL then new context is created. Parameters: s The destination buffer to append to. a The string you want to append. Returns: The concatenated strings, NULL on error. See Also: talloc_strdup() talloc_strdup_append() talloc_array_length() char* talloc_strndup (const void *t, const char *p, size_tn) Duplicate a length-limited string into a talloc chunk. This function is the talloc equivalent of the C library function strndup(3). This functions sets the name of the new pointer to the passed string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * Parameters: t The talloc context to hang the result off. p The string you want to duplicate. n The maximum string length to duplicate. Returns: The duplicated string, NULL on error. char* talloc_strndup_append (char *s, const char *a, size_tn) Append at most n characters of a string to given string. The destination string is reallocated to take strlen(s) + strnlen(a, n) + 1 characters. This functions sets the name of the new pointer to the new string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * If s == NULL then new context is created. Parameters: s The destination string to append to. a The source string you want to append. n The number of characters you want to append from the string. Returns: The concatenated strings, NULL on error. See Also: talloc_strndup() talloc_strndup_append_buffer() char* talloc_strndup_append_buffer (char *s, const char *a, size_tn) Append at most n characters of a string to given buffer. This is a more efficient version of talloc_strndup_append(). It determines the length of the destination string by the size of the talloc context. Use this very carefully as it produces a different result than talloc_strndup_append() when a zero character is in the middle of the destination string. * char *str_a = talloc_strdup(NULL, "hello world"); * char *str_b = talloc_strdup(NULL, "hello world"); * str_a[5] = str_b[5] = ' ' * * char *app = talloc_strndup_append(str_a, ", hello", 7); * char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7); * * printf("%s0, app); // hello, hello (app = "hello, hello") * printf("%s0, buf); // hello (buf = "hello world, hello") * If s == NULL then new context is created. Parameters: s The destination buffer to append to. a The source string you want to append. n The number of characters you want to append from the string. Returns: The concatenated strings, NULL on error. See Also: talloc_strndup() talloc_strndup_append() talloc_array_length() char* talloc_vasprintf (const void *t, const char *fmt, va_listap) Format a string given a va_list. This function is the talloc equivalent of the C library function vasprintf(3). This functions sets the name of the new pointer to the new string. This is equivalent to: * talloc_set_name_const(ptr, ptr) * Parameters: t The talloc context to hang the result off. fmt The format string. ap The parameters used to fill fmt. Returns: The formatted string, NULL on error. char* talloc_vasprintf_append (char *s, const char *fmt, va_listap) Format a string given a va_list and append it to the given destination string. Parameters: s The destination string to append to. fmt The format string. ap The parameters used to fill fmt. Returns: The formatted string, NULL on error. See Also: talloc_vasprintf() Format a string given a va_list and append it to the given destination string. Good for gradually accumulating output into a string buffer. Appends at the end of the string. char* talloc_vasprintf_append_buffer (char *s, const char *fmt, va_listap) Format a string given a va_list and append it to the given destination buffer. Parameters: s The destination buffer to append to. fmt The format string. ap The parameters used to fill fmt. Returns: The formatted string, NULL on error. See Also: talloc_vasprintf() Format a string given a va_list and append it to the given destination buffer. Always appends at the end of the talloc'ed buffer, not the end of the string. Author Generated automatically by Doxygen for talloc from the source code. Version 2.0 Tue Jun 17 2014 The talloc string functions.(3)
All times are GMT -4. The time now is 03:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy