Nawk sub not substituting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk sub not substituting
# 1  
Old 06-12-2014
Nawk sub not substituting

I am trying to use nawk sub to substitute a string in a file. Both the pattern and the replacement I set as variables using bash.

here is the code:
Code:
#!/bin/bash -x
ydate=`/usr/local/bin/date +%Y%m%d`
echo $ydate
test_ca=/home/mdadmin/test_ca
for i in `cat ${test_ca}`
do
if [[ $i == *cana* ]]; then
new_f=`/usr/bin/nawk -F"/" -v dt="$ydate" '{ print $5 "." dt }' $test_ca`
echo $new_f
nawk -F "/" -v y=$i,z=$new_f ' { sub(y,z,$4) ; print $0} ' ${test_ca}
fi
done

When I execute it returns
Code:
/pub/data/cana/Peoccam

I want it to return :
Code:
/pub/data/cana/Peoccam.20140612

Any suggestions are greatly appreciated. I have the bash -x output if needed.
# 2  
Old 06-12-2014
There seems to be a bit of overhead. Why are you processing each line in test_ca then running nawk against it twice. The idea behind nawk is it's a filtering language so you should only have to filter your input file once.

Simplify this and create a nawk file and use the -f option to point to it.

Post your input file test_ca.
# 3  
Old 06-12-2014
I need to execute an mv on the file to rename with the date appended. I removed the mv command from the post for simplicity.

So bascially I need to rename the file and then change the name in the test_ca file so I can sftp it.

here are the contents of test_ca -

Code:
/pub/data/cana/Peoccam

# 4  
Old 06-12-2014
Not quite sure what your requirements are without seeing an example input file and output file, but this might get you along the way:

Code:
nawk -F/ -v dt=$(/usr/local/bin/date +%Y%m%d) '
/cana/ { v=$5"."dt}
{ $NF=v ; print } ' OFS=/ /home/mdadmin/test_ca


... or even. Remove echo (in read) once your sure it's going to do what you want:

Code:
while read filename
do
    echo mv $filename $filename.$(/usr/local/bin/data +%Y%m%d)
done < /home/mdadmin/test_ca


Last edited by Chubler_XL; 06-12-2014 at 05:04 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 06-13-2014
Thanks. Seeing a different approach does help.

However, do you have any ideas why the sub is not substituing. In the original piece of code I did the following-

Code:
nawk -F "/" -v y=$i,z=$new_f ' { sub(y,z,$4) ; print $0} ' ${test_ca}

The Z is -
Code:
/pub/data/cana/Peoccam.20140612

the y is

Code:
/pub/data/cana/Peoccam

this is the output from shell-
Code:
smenago@ftp02$ bash -x test_app
++ /usr/local/bin/date +%Y%m%d
+ ydate=20140612
+ echo 20140612
20140612
+ test_ca=/home/mdadmin/test_ca
++ cat /home/mdadmin/test_ca
+ for i in '`cat ${test_ca}`'
+ [[ /pub/data/cana/Peoccam == *cana* ]]
++ /usr/bin/nawk -F/ -v dt=20140612 '{ print $5 "." dt }' /home/mdadmin/test_ca
+ new_f=Peoccam.20140612
+ echo Peoccam.20140612
Peoccam.20140612
+ mv /pub/data/cana/Peoccam /pub/data/cana/Peoccam.20140612
mv: cannot access /pub/data/cana/Peoccam
+ nawk -F / -v y=/pub/data/cana/Peoccam,z=Peoccam.20140612 ' { sub(y,z,$4) ; print $0} ' /home/mdadmin/test_ca
/pub/data/cana/Peoccam


Last edited by Scrutinizer; 06-13-2014 at 12:13 PM.. Reason: Additional code tags
# 6  
Old 06-13-2014
Quote:
Originally Posted by smenago
Thanks. Seeing a different approach does help.

However, do you have any ideas why the sub is not substituing. In the original piece of code I did the following-

nawk -F "/" -v y=$i,z=$new_f ' { sub(y,z,$4) ; print $0} ' ${test_ca}
The Z is -
/pub/data/cana/Peoccam.20140612

the y is

/pub/data/cana/Peoccam
this is the output from shell-
Code:
smenago@ftp02$ bash -x test_app
++ /usr/local/bin/date +%Y%m%d
+ ydate=20140612
+ echo 20140612
20140612
+ test_ca=/home/mdadmin/test_ca
++ cat /home/mdadmin/test_ca
+ for i in '`cat ${test_ca}`'
+ [[ /pub/data/cana/Peoccam == *cana* ]]
++ /usr/bin/nawk -F/ -v dt=20140612 '{ print $5 "." dt }' /home/mdadmin/test_ca
+ new_f=Peoccam.20140612
+ echo Peoccam.20140612
Peoccam.20140612
+ mv /pub/data/cana/Peoccam /pub/data/cana/Peoccam.20140612
mv: cannot access /pub/data/cana/Peoccam
+ nawk -F / -v y=/pub/data/cana/Peoccam,z=Peoccam.20140612 ' { sub(y,z,$4) ; print $0} ' /home/mdadmin/test_ca
/pub/data/cana/Peoccam

Looks like variable you properly not defined

see difference here in example :

Code:
$ awk -vx=1,y=2 'BEGIN{print y}'

$ awk -vx=1,y=2 'BEGIN{print x}'
1,y=2

$ awk -vx=1 -vy=2 'BEGIN{print x,y}'
1 2

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 06-13-2014
Also even if you get y and z assigned properly, lets expand out what sub(y,z,$4) is seeing:

Code:
sub("/pub/data/cana/Peoccam", "Peoccam.20140612",  "cana")

So substitute "/pub/data/cana/Peoccam" with "Peoccam.20140612" in string "cana"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

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

3. UNIX for Dummies Questions & Answers

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 ;... (3 Replies)
Discussion started by: ganesh_248
3 Replies

4. Shell Programming and Scripting

Substituting the values

Hi Gurus this is working finee with tested values #!/bin/ksh V_DATE="2007-11-30" V_ID=789 V_NAME="john_${V_ID}_has_${V_DATE}_s" FILE_NAME=`echo ${V_NAME}` echo ${FILE_NAME} Buttt the problem is the first two values will come dynamically and the file will looks like... (2 Replies)
Discussion started by: SeenuGuddu
2 Replies

5. Shell Programming and Scripting

Substituting Characters using SED

Can SED be used to substitute a character (y) with a character (Y) in a specified field? File has 12000 : delimeted rows as; HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT HHC 2 BDE:Lastname, Firstname MI:Firstname.MI.Lastname@mil:SGT I wish to replace the capital letters... (6 Replies)
Discussion started by: altamaha
6 Replies

6. Programming

substituting one string for another

I have a Linux C program I'm writing that has one section where, within a large string, I need to substitute a smaller string for another, and those probably won't be the same size. For instance, if I have a string: "Nowisthetimeforallgoodmen" and I want to substitute 'most' for 'all' the... (2 Replies)
Discussion started by: cleopard
2 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Shell Programming and Scripting

substituting

Hello please how can i change this infrormation within a file dynamically without using vi " || $6 ~ /^229*/ " the * means any number within the file has this content : cat lec|awk -F '|' 'length($6) >= 12 || length($6) <= 10' |awk -F '|' '$6 ~ /^24/ || $6 ~ /^22924/ &&$7 ~... (1 Reply)
Discussion started by: neyo
1 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 shell variables

I have a script run_batch.sh as below :- PAR_VALIDATION=val_siconf PAR_RUN_LEVEL=1 PAR_EXCLUSIVE_RUN_YN=Y DATABASE_USER="/@"$TWO_TASK sqlplus -s $DATABASE_USER |& print -p -- 'set feed off pause off pages 0 head off veri off line 500' print -p -- 'set term off time off... (1 Reply)
Discussion started by: suds19
1 Replies
Login or Register to Ask a Question