awk Print get error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Print get error
# 1  
Old 06-22-2011
awk Print get error

Hi, Gurus,
I have script as following:
Code:
#!/usr/bin/khs
lastdate=`cat abc`
echo $lastdate
awk '{ if ($0~/^\$\$lastupdatedate *=/) {print '\$\$lastupdatedate=$lastdate'} else {print $0}}' file1 > tmp

error message
Code:
awk: syntax error near line 1
awk: illegal statement near line 1

when I change $lastdate to '2001-01-01 00:00:00', code as following:
Code:
#!/usr/bin/khs
lastdate=`cat abc`
echo $lastdate
awk '{ if ($0~/^\$\$lastupdatedate *=/) {print "$$lastupdatedate=2001-01-01 00:00:00"} else {print $0}}' file1 > tmp

Smilie

Any body can help me out

Thanks in advance
ken002
# 2  
Old 06-22-2011
That's a useless use of backticks. Why do "var=`cat stuff` ; echo $var" when just "cat stuff" achieves the same thing?

It's valid in my awk. Depending on your system, 'awk' may actually be an ancient feature-deprived version, try nawk.

I'm not sure it's actually doing what you want either, though. That $$ may be turning into a number, the shell's PID.

There's an easier and safer way to get variables into awk/nawk than fooling with single/double quotes:

Code:
echo asdf | awk -v var="qwerty" '{ print $1, var }'

---------- Post updated at 01:23 PM ---------- Previous update was at 01:15 PM ----------

Or even get the var in awk itself:
Code:
awk 'BEGIN { getline var <"abc" } { print $0, var }'


Last edited by Corona688; 06-22-2011 at 04:26 PM.. Reason: fix typos, sorry
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-22-2011
Also, you probably should fix your shebang line:
Code:
#!/usr/bin/ksh


# 4  
Old 06-22-2011
Thanks both of you.
My situation is:
In my first file abc contains one string '2011-01-01' (only on line), in my second file contains multiple lines, one of them is $$lastupdatedate=xxxx-xx-xx, I want replace xxxx-xx-xx with 2011-01-01 in second file and get $$lastupdatedate=2011-01-01.

Any body can help me out.

Thanks in advance
ken002
# 5  
Old 06-22-2011
Code:
#!/bin/ksh

dateFile=~/test/date.txt
fileToReplace=~/test/input.txt

awk -F= '/^\$\$lastupdatedate/ {$2="'`cat $dateFile`'"}1' OFS="=" $fileToReplace

the regex will match a line starting with '$$lastupdatedate', and replace the second field (stuff after equal sign) with the contents of $dateFile. '1' at the end is just a shortcut to print everything out.
This User Gave Thanks to mirni For This Post:
# 6  
Old 06-23-2011
Thanks Mirni for your detail explanation.

It works perfectly.

---------- Post updated 06-23-11 at 10:51 AM ---------- Previous update was 06-22-11 at 09:10 PM ----------

Hi, Mirni,
Above scripts works fine when I tried on my laptop. But it give me error when I tested on my workstation.
awk: syntax error near line 1
awk: illegal statement near line 1
the system as following
$ uname -a
SunOS abc 5.10 Generic_144488-11 sun4v sparc SUNW,Sun-Fire-T200

any idea about this?

Thanks in advance
ken002
# 7  
Old 06-23-2011
Use nawk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print Error in Console and both Error & Output in Log file - UNIX

I am writing a shell script with 2 run time arguments. During the execution if i got any error, then it needs to redirected to a error file and in console. Also both error and output to be redirected to a log file. But i am facing the below error. #! /bin/sh errExit () { errMsg=`cat... (1 Reply)
Discussion started by: sarathy_a35
1 Replies

2. Shell Programming and Scripting

Awk: Print Error While Redirecting output in multiple Files

Hi, I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") { array1++ array2++ array3++ } else if (substr($1,26,2)=="03" &&... (4 Replies)
Discussion started by: siramitsharma
4 Replies

3. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

AWK print AWK command

awk '{print "awk '{sub(/pdb_00/,"pdb_"$0"_00"); print}' pdb_"$0"_00.namd > tempo"; print "mv tempo pdb_"$0"_00.namd"}' datA2.dat > copy_script2.bash This works when trying to print 'sed etc. etc' but if I switch to using AWK to print a set of AWK commands it doesn't work... e.g. this... (3 Replies)
Discussion started by: chrisjorg
3 Replies

6. Shell Programming and Scripting

Print using awk

Hi.. I have two files. contents of the files are as below. header1.dat ------------- This is a header record. header2.dat ------------- This is a header record. Date:2011-01-05 I am executing the below scripts. HEADER1=`cat header1.dat` HEADER2=`cat header2.dat` awk 'BEGIN... (2 Replies)
Discussion started by: 46019
2 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

awk - print

Hi all, I have script below: #!/bin/sh $name=blah awk -F: -v user="$name" '$1 == user{print $1 ":" $2 ":" $3 ":" $4}' /etc/shadow > /tmp/shadow.tmp based on above, it will only print one line which is "blah" username details. What do i need to do to print all contents of shadow... (6 Replies)
Discussion started by: c00kie88
6 Replies

9. Shell Programming and Scripting

i want to print following using awk

Hi all I have taken input $fname=filename.345.txt Now i want to awk with using delimiter as '.' and print last word. i.e in this case it is "txt". I have tired like this echo $filename | awk -F"." '{print $END}' but it is not working. Can any one help me Thank U Naree (2 Replies)
Discussion started by: naree
2 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question