Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to assign a value to a variable in awk scripting? Post 303038209 by Don Cragun on Tuesday 27th of August 2019 08:47:55 PM
Old 08-27-2019
Quote:
Originally Posted by bhagya123
Thanks..Actually i tried with substr and it was working..

Later i encountered an error and thought that is related to to the substr function. But looks like it caused by something else.

Thanks for the response !!

--- Post updated at 07:33 PM ---

Similar to that I am also trying an awk array and wanted to check if the array value match string "{5:{" , replace with 5: and assign it back to same array and print it.

But below code is not working its not able to replace the array value , instead its assigning whole command to msg_arr[i] and printing it

any solution for this?

Code:
              for (i=msgtkn_20_lnno; i<msg_lnno;)
              {
                if(msg_arr[i]  ~ /^{5:{/ )
                {
                tag5_before_value= msg_arr[i];
               msg_arr[i]="echo $tag5_before_value | sed 's/{5:{/5:/g'";
                printf "after conversion is %s/n", abc > "LOG.txt";
               print msg_arr[i++];
               }
                else
                {
                  print msg_arr[i++];
                }

              }

as soon as i use "`" its giving me syntax error
Moderator's Comments:
Mod Comment Please do not use two threads to discuss one topic.

Please do not use one thread to discuss two topics. The second topic in this thread is already being discussed in the thread sed inside the awk script to replace a string in the array. Do not continue that discussion here!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk: assign a printf value to a variable

Is there any way to something like this?: variable=printf("%30s",var1) Thx. (2 Replies)
Discussion started by: Klashxx
2 Replies

2. Shell Programming and Scripting

assign value to variable using AWK

Dear Friends I have text file as like below, AAAAA|BHBHBH|VERYSMART AAAAA| KKKKKK|GOOD BBBBBB|JJJJJJJ|VERYGOOD CCCCC|HJHJHJ|BETTER CCCCC|UUUUU|GOOD i need to split into seperate files based on column 1 like as below AAAAA.TXT contains -------------------- BHBHBH.VERYSMART... (4 Replies)
Discussion started by: HAA
4 Replies

3. Shell Programming and Scripting

Assign o/p of awk to a variable

:confused: Hi UNIX gurus, I am facing a typical problem while assigining while assigining output of awk to a variable. I have a fixed length file say myinputfile.txt When I allow the value/output of an awk to be redirected to a file, it works fine. i.e. awk "/^.{232}$acctNum/ {... (8 Replies)
Discussion started by: c2b2
8 Replies

4. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

5. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. Shell Programming and Scripting

help on awk---- need to assign the output of awk to a variable

hi i want to find the size of a folder and assign it to a variable and then compare if it is greater than 1 gb. i am doin this script, but it is throwing error.... #!/bin/ksh cd . | du -s | size = awk '{print $1}' if size >= 112000 then echo size high fi ERROR : (4 Replies)
Discussion started by: Nithz
4 Replies

8. Shell Programming and Scripting

Assign a variable with awk

I launch 'netstat -a', if string 'ESTABLISHED' found, then VAR=1 #!/bin/bash VAR=0; netstat -a | awk '$6 ~ /ESTABLISHED/ {VAR=1}' I cannot find the right syntax. thanx guys! (3 Replies)
Discussion started by: arpagon
3 Replies

9. Shell Programming and Scripting

Assign awk gsub result to a variable

Hello, I have searched but failed to find what exactly im looking for, I need to eliminate first "." in a output so i can use something like the following echo "./abc/20141127" | nawk '{gsub("^.","");print}' what i want is to use gsub result later on, how could i achieve it? Let say... (4 Replies)
Discussion started by: EAGL€
4 Replies

10. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies
STR_REPLACE(3)								 1							    STR_REPLACE(3)

str_replace - Replace all occurrences of the search string with the replacement string

SYNOPSIS
mixed str_replace (mixed $search, mixed $replace, mixed $subject, [int &$count]) DESCRIPTION
This function returns a string or an array with all occurrences of $search in $subject replaced with the given $replace value. If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace(3). PARAMETERS
If $search and $replace are arrays, then str_replace(3) takes a value from each array and uses them to search and replace on $subject. If $replace has fewer values than $search, then an empty string is used for the rest of replacement values. If $search is an array and $replace is a string, then this replacement string is used for every value of $search. The converse would not make sense, though. If $search or $replace are arrays, their elements are processed first to last. o $search - The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles. o $replace - The replacement value that replaces found $search values. An array may be used to designate multiple replacements. o $subject - The string or array being searched and replaced on, otherwise known as the haystack. If $subject is an array, then the search and replace is performed with every entry of $subject, and the return value is an array as well. o $count - If passed, this will be set to the number of replacements performed. RETURN VALUES
This function returns a string or an array with the replaced values. EXAMPLES
Example #1 Basic str_replace(3) examples <?php // Provides: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // Provides: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); // Provides: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; ?> Example #2 Examples of potential str_replace(3) gotchas <?php // Order of replacement $str = "Line 1 Line 2 Line 3 Line 4 "; $order = array(" ", " ", " "); $replace = '<br />'; // Processes 's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); // Outputs F because A is replaced with B, then B is replaced with C, and so on... // Finally E is replaced with F, because of left to right replacements. $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); // Outputs: apearpearle pear // For the same reason mentioned above $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text); echo $output; ?> NOTES
Note This function is binary-safe. Caution Replacement order gotcha Because str_replace(3) replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document. Note This function is case-sensitive. Use str_ireplace(3) for case-insensitive replace. SEE ALSO
str_ireplace(3), substr_replace(3), preg_replace(3), strtr(3). PHP Documentation Group STR_REPLACE(3)
All times are GMT -4. The time now is 07:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy