awk gsub simple problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk gsub simple problem
# 1  
Old 11-08-2009
awk gsub simple problem

Hi

New to shell script and awk and need assistance on this problem. I need to use a variable to substitute a string in an external file and write the changed info to another file.

At first I did not know if you could use a variable as the sub value but the following showed me that I can.

echo "HI FROM STEVE" | awk '{ print $2; gsub(/STEVE/, $2); print }'
FROM
HI FROM FROM

This is the code that I have

ls -ltd @EOD4401* | sort +9 | head -n1 |
awk ' {{ {print $9 } print } ; gsub(/jobname/,$9 ); print }'
BPO_File_Template > cvtestfile

I have multiple directories as @EOD4401_0001, @EOD4401_0002 etc etc and I need to get the highest one and simply replace all text "jobname" in the file BPO_FILE_Template and write out to cvtestfile

The template file has

/mydir1/jobname
/midir2/jobname

but what gets written to cvtestfile is

/midir1/
/mydir2/

What am I doing wrong?

Thanks in advance






Last edited by hukcjv; 11-08-2009 at 06:13 PM.. Reason: update text
# 2  
Old 11-08-2009
You are feeding awk from two sides: stdin and the file input from BPO_File_Template. Apparently in that case stdin gets ignored:
Code:
$> awk '{print}' <(echo 1 2 3 4)
1 2 3 4

$> echo "a b c d"|awk '{print}'
a b c d

$> echo "a b c d"|awk '{print}' <(echo 1 2 3 4)
1 2 3 4

# 3  
Old 11-09-2009
Thanks for the reply. At least it stops me banging my head against the wall. Have removed awk and simply "cut" the column I need

CMD1="ls -ltd @EOD4401* | sort +9 | head -n1 "
LINE1=`eval $CMD1`

jobname_rep=`echo $LINE1 | cut -d ' ' -f9`

sed -e "s/jobname/$jobname_rep/g" -e "s/0000/4401/g" BPO_File_Template > cvtestfile


Am sure I can make it simpler but this works for me..

Still would be interested if awk could do it
# 4  
Old 11-09-2009
Something like this?

Code:
awk ' BEGIN {"ls -ltd @EOD4401* | sort +9 | head -n1" | getline cmd; split(cmd, a)} {
  gsub("jobname", a[9]); gsub("0000", "4401")
}1' BPO_File_Template > cvtestfile


Last edited by Franklin52; 11-09-2009 at 07:51 AM.. Reason: typo, removed ")"
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem using gsub in gawk

I know that most of you guys probably won't reply to this, because you'll feel like my post doesn't even deserve a glance. However, for those of you that do decide to help- thank you! TRAILS TA 2700 E Main St ALBERT LEA , MN 56007 507-373-4200 TRUCKER'S INN State Hwy 30 AT I 35 N... (6 Replies)
Discussion started by: seanmonaco
6 Replies

2. Shell Programming and Scripting

Simple awk search problem

Hello; we have : awk '/reg_exp/,0/ prints every line after the first occurrence of "reg_exp" But if I want to print rest of the lines AFTER the last occurrence of "reg_exp", how would I do it ?? Tried : awk ' ! (/reg_exp/,0)' But it errored... Thank you for any... (5 Replies)
Discussion started by: delphys
5 Replies

3. Shell Programming and Scripting

simple awk sort problem

Hello folks I have the following output UNIX95=1 ps -ef -o pcpu,user,pid,args |more %CPU USER PID COMMAND 0.03 root 0 swapper 0.08 root 1 init 0.00 root 13 net_str_cached 0.00 root 12 usbhubd 0.00 root 11 escsid 0.00 root 10... (3 Replies)
Discussion started by: delphys
3 Replies

4. Shell Programming and Scripting

another simple awk problem

Hello; I need to print two previous lines after searching for a reg exp: awk '/haywood/' should produce the following =================== p9J46THe020804 89922 Tue Oct 18 21:06 MAILER-DAEMON (host map: lookup (haywood.com): deferred) ... (1 Reply)
Discussion started by: delphys
1 Replies

5. Shell Programming and Scripting

Simple awk problem II

Hello; Trying to figure out how to keep just the contents between the two search lines: awk '/regexp_1/ ,/regexp_2/' I do not want lines containing regexp_1 and regexp_2 in the output. Thank you for any ideas Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: delphys
5 Replies

6. Shell Programming and Scripting

simple awk problem

Hello; I have the following log file: 10/11/11 10:42:02 LOCK Q Userid:284 Username=root UserPID:23158 Device:marlin batch 10/11/11 10:42:02 TableNr:226 TableName:iatkn RecId:116290398 Flags:X Q H 10/11/11 10:42:02 LOCK CONTENTION X 10/11/11 10:42:02 ... (3 Replies)
Discussion started by: delphys
3 Replies

7. Shell Programming and Scripting

simple awk problem

pcn linus> ntpq -p remote refid st t when poll reach delay offset disp ============================================================================== +smpnn01 ntpsrv1 2 u 829 1024 377 1.46 0.793 0.85 *smpnn02 ntpsrv1 2 u ... (2 Replies)
Discussion started by: arch12
2 Replies

8. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

9. Shell Programming and Scripting

Help with AWK and gsub

Hello, I have a variable that displays the following results from a JVM.... 1602100K->1578435K I would like to collect the value of 1578435 which is the value after a garbage collection. I've tried the following command but it looks like I can't get the > to work. Any suggestions as... (4 Replies)
Discussion started by: npolite
4 Replies
Login or Register to Ask a Question