Issues running an awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues running an awk script
# 8  
Old 05-09-2012
It prints


Code:
mdmdev:/usr/chandra# awk 'BEGIN { print "hello world" }'
hello world


Last edited by Scrutinizer; 05-11-2012 at 03:21 AM..
# 9  
Old 05-09-2012
The test.awk file is in DOS format get rid of the CTRL-M characters on the end of each line and CTRL-Z at end of file.

Code:
tr -d "\015\032" < test.awk > test_fixed.awk


Last edited by Chubler_XL; 05-10-2012 at 12:11 AM..
# 10  
Old 05-10-2012
Wow. That worked. Thanks. Can you please explain what was done? I did not understand the code
Code:
sed 's/'`printf "\015"`'//g' test.awk > test_new.awk

.

Also What should I do to avoid this error if I create a new file to compile in awk?

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use [code]...[/code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 05-11-2012 at 03:21 AM.. Reason: code tags
# 11  
Old 05-10-2012
I updated to a slightly simpler version, but basically you need to use sed (or tr) to remove all CTRL-M characters.

This is because DOS has \r\n on the end of each line while unix only has \r.

So if you edit/create a file on Windows/DOS and transfer to unix use you should either:
  • use text mode of FTP when transfering
  • use the dosread AIX tool
  • or use the tr/sed commands I posted
This User Gave Thanks to Chubler_XL For This Post:
# 12  
Old 05-11-2012
Next try taking the closing ripple bracket to another line thus:
Code:
BEGIN {
requestID=100;
ORS="";
} {
requestID=requestID+1;
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?><RequestControl>
<requestID>"requestID"</requestID></RequestControl>";
print "<TCRMTx><AgreementName>",substr($0,36),"</AgreementName><ValueString>",$2",</ValueString>";
print "<AdminPartyId>"$1"</AdminPartyId>\n";
} END { }

Doesn't the function substr take 3 argument substr($0,1,36) or left($0,36)?

What happens if the length of $0 is < 36?

Last edited by Scrutinizer; 05-11-2012 at 03:25 AM.. Reason: code tags instead of quote tags
# 13  
Old 05-11-2012
Quote:
Originally Posted by Chubler_XL
I updated to a slightly simpler version, but basically you need to use sed (or tr) to remove all CTRL-M characters.

This is because DOS has \r\n on the end of each line while unix only has \r.

[..]
I agree this is the most likely cause. The awk code itself does not need the be changed. Just a minor detail (I know you know, but for the sake of this thread), Unix files only have line feeds (\n), and do not use carriage returns (\r).

--
This should usually suffice:
Code:
tr -d '\r' < infile > outfile

If a file is in DOS-format, it could for example be tested with case (in shell):
Code:
case $line in 
  *^M) echo "$line ends in a carriage return"
esac

^M is entered as CTRL-V <RETURN>

Last edited by Scrutinizer; 05-11-2012 at 04:01 AM..
# 14  
Old 05-11-2012
My Output in the text file output.txt is getting printed thrice now.

The input is,

Code:
2012-01,S27,GC00000T

command used:

Code:
awk -f test.awk input.txt > output.txt

Script:

Code:
BEGIN{
FS=",";
requestID=100;
ORS="";
}{ ..... }
END{

}

Moderator's Comments:
Mod Comment Code tags for code, please.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running awk script in Debian 6.0.2

Hello to all, May be some expert could help me. I have the below awk script that works correctly in Cygwin: awk -F\" 'FNR==NR && FNR>1 {gsub(",","",$5); N=$5;next} $10 in N && FNR>1 { Y=$8 Z=(N==$8)?"Yes:"No"; } END{ for(k in N) {print k,Y,N,Z?Z:"Not_Found"} }' file2 file1But when I... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

2. UNIX for Dummies Questions & Answers

Facing issues while running a cronjob !

Hi, I am trying to run a cronjob. But while doing so I am getting the following error message :- can't open yourfile in /var/spool/cron/crontabs directory. No such file or directory How can I resolve this issue ? Please help. Thanks Please view this code tag video for... (14 Replies)
Discussion started by: acidburn_007
14 Replies

3. UNIX for Dummies Questions & Answers

awk: issues for writing a script

%%%%% (7 Replies)
Discussion started by: lucasvs
7 Replies

4. Shell Programming and Scripting

Problem with running awk script in pbs file

Hi guys I have a problem with one module of my script. It's awk script running in pbs file - It's working when I make a call from pbs to separate awk only file like this but when I try to execute this code in pbs file it fails awk 'BEGIN { max = -1000; min = 1000 } { {$4 == "TCO"} ... (2 Replies)
Discussion started by: grincz
2 Replies

5. Shell Programming and Scripting

AWK - Issues with script running at start of month.

I have a script that runs on an AIX 5.3.10.0. It runs perfectly on days that are double digit numbers, but from the 1st to the 9th of the month it runs but does not report anything. The command in the script is as follows: awk -v d=$(date '+%b%d') '/user:warn/ && /has shutdown/ && ($1$2 ==... (2 Replies)
Discussion started by: jimbojames
2 Replies

6. UNIX for Dummies Questions & Answers

swap issues, system is running at 99%

Hi All, I am trying to understand why my system is running at very high. This system is almost out of memory. See below. swapon -s Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol02 partition 8388600 8235088 -1... (2 Replies)
Discussion started by: samnyc
2 Replies

7. Red Hat

postgresql issues - system running really slow

Hi All, I'm new here. i was wondering if anyone could shed a light on the problem i am having. I use a system for distributing broadband amongst users of for example a hotel, the system was designed by someone in the US and it is based on redhat 2.4 (i know its old) and the system uses... (3 Replies)
Discussion started by: clive306
3 Replies

8. Shell Programming and Scripting

Issues with exit after running jobs in background

I have the following sample script to run a script the jobs with the same priority(in this case field3) in parallel; wait for the jobs to finish and run the next set of jobs in parallel.When all the lines are read exit the script. I have the following script which is doing evrything I want... (1 Reply)
Discussion started by: hyennah
1 Replies

9. UNIX for Advanced & Expert Users

Problem in running an AWK script

Hi Everyone, I am required to write an AWK script that can be run from any directory. This script finds subscription rates for each subscriber at an interval of 10 mins. I want that just by copy pasting the whole script in shell prompt and giving the input arguments, script must work. Currenty... (4 Replies)
Discussion started by: rachana8p
4 Replies

10. Shell Programming and Scripting

is running this command via ssh possible? (formatting issues)

Here is the command I want to run: for pkg in `pkginfo | grep -i VRTS | awk '{print $2}'`; do showrev -p | grep $pkg; done | awk '{print $2 "\t" $7}' | uniq It returns the package info in a form such as: 113210-03 VRTSfspro 112392-06 VRTSvmman 113596-03 VRTSvmpro... (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question