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

              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++];
                  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!
Test Your Knowledge in Computers #854
Difficulty: Medium
React (also known as React.js or ReactJS) is a JavaScript library maintained by Facebook for building user interfaces.
True or False?

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
JRUBY(1)							       LOCAL								  JRUBY(1)

jruby -- Interpreted object-oriented scripting language SYNOPSIS
jruby [--copyright] [--version] [-Sacdlnpswvy] [-0[octal]] [-C directory] [-F pattern] [-I directory] [-K c] [-T[level]] [-e command] [-i[extension]] [-r library] [-x[directory]] [--] [program_file] [argument ...] DESCRIPTION
Jruby is a 100% pure-Java implementation of Ruby, an interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, and extensible. OPTIONS
Ruby interpreter accepts following command-line options (switches). They are quite similar to those of perl(1). --copyright Prints the copyright notice. --version Prints the version of Ruby interpreter. -0[octal] (The digit ``zero''.) Specifies the input record separator ($/) as an octal number. If no digit is given, the null character is taken as the separator. Other switches may follow the digits. -00 turns Ruby into paragraph mode. -0777 makes Ruby read whole file at once as a single string since there is no legal character with that value. -C directory Causes Ruby to switch to the directory. -F pattern Specifies input field separator ($;). -I directory Used to tell Ruby where to load the library scripts. Directory path will be added to the load-path variable ($:). -K kcode Specifies KANJI (Japanese) encoding. -S Makes Ruby use the PATH environment variable to search for script, unless if its name begins with a slash. This is used to emulate #! on machines that don't support it, in the following manner: #! /usr/local/bin/ruby # This line makes the next one a comment in Ruby exec /usr/local/bin/ruby -S $0 $* -T[level] Turns on taint checks at the specified level (default 1). -a Turns on auto-split mode when used with -n or -p. In auto-split mode, Ruby executes $F = $_.split at beginning of each loop. -c Causes Ruby to check the syntax of the script and exit without executing. If there are no syntax errors, Ruby will print ``Syntax OK'' to the standard output. -d --debug Turns on debug mode. $DEBUG will be set to true. -e command Specifies script from command-line while telling Ruby not to search the rest of arguments for a script file name. -h --help Prints a summary of the options. -i extension Specifies in-place-edit mode. The extension, if specified, is added to old file name to make a backup copy. For example: % echo matz > /tmp/junk % cat /tmp/junk matz % ruby -p -i.bak -e '$_.upcase!' /tmp/junk % cat /tmp/junk MATZ % cat /tmp/junk.bak matz -l (The lowercase letter ``ell''.) Enables automatic line-ending processing, which means to firstly set $ to the value of $/, and secondly chops every line read using chop!. -n Causes Ruby to assume the following loop around your script, which makes it iterate over file name arguments somewhat like sed -n or awk. while gets ... end -p Acts mostly same as -n switch, but print the value of variable $_ at the each end of the loop. For example: % echo matz | ruby -p -e '$! "a-z", "A-Z"' MATZ -r library Causes Ruby to load the library using require. It is useful when using -n or -p. -s Enables some switch parsing for switches after script name but before any file name arguments (or before a --). Any switches found there are removed from ARGV and set the corresponding variable in the script. For example: #! /usr/local/bin/ruby -s # prints "true" if invoked with `-xyz' switch. print "true " if $xyz On some systems $0 does not always contain the full pathname, so you need the -S switch to tell Ruby to search for the script if necessary. To handle embedded spaces or such. A better construct than $* would be ${1+"$@"}, but it does not work if the script is being interpreted by csh(1). -v --verbose Enables verbose mode. Ruby will print its version at the beginning, and set the variable $VERBOSE to true. Some methods print extra messages if this variable is true. If this switch is given, and no other switches are present, Ruby quits after printing its version. -w Enables verbose mode without printing version message at the beginning. It sets the $VERBOSE variable to true. -x[directory] Tells Ruby that the script is embedded in a message. Leading garbage will be discarded until the first that starts with ``#!'' and contains the string, ``ruby''. Any meaningful switches on that line will applied. The end of script must be spec- ified with either EOF, ^D (control-D), ^Z (control-Z), or reserved word __END__. If the directory name is specified, Ruby will switch to that directory before executing script. -y --yydebug Turns on compiler debug mode. Ruby will print a bunch of internal state messages during compiling scripts. You don't have to specify this switch, unless you are going to debug the Ruby interpreter. UNIX
Apr 2, 2007 UNIX

Featured Tech Videos

All times are GMT -4. The time now is 09:00 PM.
Unix & Linux Forums Content Copyright 1993-2020. All Rights Reserved.
Privacy Policy