substituting variable value in AWK

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers substituting variable value in AWK
# 1  
Old 07-11-2011
MySQL substituting variable value in AWK

Hi All,

my requirement is as below.

I need to replace a value in a particular column with a substitution variable(date value) and modified value of the current column value in the same position.

for ex.

i have a record like

02;aaaa;bbbbb;cccccc;dddddd;123456789;hhhhh;12hs;asdf ;

i need to insert date value(whiic is obtained from another record and stored in a variable $dt=20110702) at 6th column(123456) and also modify the current value as 123XXX789.

o/p should be like


i need to modify my below code with AWK so that i can substitute the date value (which is stored in a variable $dt) in the 6th column
cat test1 | grep "02" > out
nawk '
  {$6=substr($6,1,3) "XXX" substr($6,7)}
  1' out >> ofile
  rm out

The file is a huge file and i do not want to open the file and change each line while i need to do some tweeking in the existing code to incorporate this change.Any help is greatly appreciated.

Last edited by vbe; 07-11-2011 at 01:18 PM.. Reason: code tags please
# 2  
Old 07-11-2011
You can set variables in awk with
awk -v VAR="$value" ..

You've got a useless use of cat in there, and a useless temp file. Instead of 'cat file | grep string' you can just do 'grep string file', and instead of 'grep string file > out ; nawk out ...' you can do 'grep string file | awk ...'
# 3  
Old 07-11-2011
Hi Corona,

Can you modify my existing code with a your suggestions.

# 4  
Old 07-11-2011
Don't need the grep at all, awk is for pattern matching! Such as

nawk 'BEGIN { FS=OFS=";" } $1 == "02" {$6=dt OFS substr($6,1,3) "XXX" substr($6,7); print}' dt=$dt test1

Your example from command line:
[mute@sunny ~]$ echo "02;aaaa;bbbbb;cccccc;dddddd;123456789;hhhhh;12hs;asdf ;" | nawk 'BEGIN { FS=OFS=";" } $1 ~ "02" {$6=dt OFS substr($6,1,3) "XXX" substr($6,7); print}' dt=20110702
02;aaaa;bbbbb;cccccc;dddddd;20110702;123XXX789;hhhhh;12hs;asdf ;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

getting error while substituting variable using sed..

I am using script for substitute one variable with another variable like below... below code works fine... sed 's/'$sub_fun'/'$To_sub'/g' But when i run the same code from the script getting below errors.. sed: -e expression #1, char 7: unterminated `s' command please help....... (2 Replies)
Discussion started by: pamu
2 Replies

2. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

3. Shell Programming and Scripting

Substituting a shell variable in sed

Hi guys, I'm trying to figure out how to use a shell variable inside my sed command. I just want to remove a certain part of a path. I've tried three different combinations and none of them work. Here are the three combinations: echo $file | sed 's/'$test'//' echo $file | sed "s/$test//"... (7 Replies)
Discussion started by: chu816
7 Replies

4. UNIX for Dummies Questions & Answers

Variable is not substituting values

Hi All, OS HPUX 11.11 I am using following script to take controlfile backup. I have used SID variable to hold "ffin1" value, which I again subsitute in "'/db/ffin1/home/oraffin1/$SID_$wdate.ctl'" command. Well, after running this, SID variable does not subsittue it's value, while wdate... (6 Replies)
Discussion started by: alok.behria
6 Replies

5. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

6. Shell Programming and Scripting

Substituting a word by different word in variable

Hello Exprets, I have a requirement where i have to subtitue a word with another word. $eachline| sed 's/*$//' a) $eachline will hold a value a path for example user/oracle/Test_admin/myfolder/bakup/part_bkptemp_part_bkp_repeated/list.txt b) $eachline| sed 's/*$//' will give me the... (3 Replies)
Discussion started by: aks_1902
3 Replies

7. Shell Programming and Scripting

Substituting field contents using AWK

Hello, wondering if anybody may be help me. This is the output of a file, from which I need to display a number of fields regarding which users are using licences for two applications we run. Lines which end with "(linger: 1800)" denote licence use for one application and lines which don't contain... (8 Replies)
Discussion started by: Glyn_Mo
8 Replies

8. Shell Programming and Scripting

Substituting variable value in AWK /start/,/stop/

Hi all u brilient people on the forum... I am trying to call the variable value in awk command for search pattern /start/,/stop/ but i am nt able to do this .... wat i did is ..i have created two variable YESTERDAY and TODAY and passed the y'day n 2'days dates in this ... (14 Replies)
Discussion started by: whomi
14 Replies

9. Shell Programming and Scripting

Substituting with value of variable in Sed

Hi, I have a program in which i have to substitute a TAG in a file with the value of a variable. Code Snippet: ---------------- myvar=1234 sed 's/_TAG_/$myvar/' infile outfile When I run this command, the _TAG_ in the "infile" is substituted with "$myvar" but NOT the value "1234"... (1 Reply)
Discussion started by: jyotipg
1 Replies

10. Shell Programming and Scripting

Substituting variable with current time

Hi all I have a script as follows :- #!/usr/bin/ksh IDT=`date +"%OH%M%S"` while true do echo ${IDT} sleep 1 done I need the time to show me the current runtime value for the time, however this returns the time as at the start of the script. Any ideas. Thanks JH (4 Replies)
Discussion started by: jhansrod
4 Replies
Login or Register to Ask a Question