awk command not working from the shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command not working from the shell script
# 1  
Old 12-06-2009
awk command not working from the shell script

Hi,

When i run the below command i am able to get the output.

Code:
awk '/BEGIN DSSUBRECORD/{c=3;next}c-->0' abc.txt | 
awk '/END DSSUBRECORD/{exit}{print}' |
awk '/Owner/{exit}{print}' | 
awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}'

Output:
Code:
 Name "file_name",          Prompt "Enter the file name",          ParamType "1"
 Name "file_name1",         Prompt "Enter the file name",          ParamType "1"

==================================================================================================== ========

I want run this command for multiple files. so i wrote a script to check.
When i run this command using a sheel script, it is not working.

I have the below error message:

Code:
a=0
cnt=2
a1="abc.txt"
while [ $a -ne $cnt ]
do
bp1="awk '/BEGIN DSSUBRECORD/{c=3;next}c-->0'  | 
awk '/END DSSUBRECORD/{exit}{print}' |
awk '/Owner/{exit}{print}' | 
awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}'"
echo " "  `$bp1`
a=$(( $a + 1 ))
done
 
 Syntax Error The source line is 1.
 The error context is
                 >>> /BEGIN <<<
 awk: 0602-500 Quitting The source line is 1.



input is a txt file and it is a big file.i tired all the possibilties to skip the '/' and $0 values in the command.
Any help would be appreciated.

Thanks

Last edited by Franklin52; 12-06-2009 at 07:56 AM.. Reason: Please use code tags!!
# 2  
Old 12-06-2009
It would help if you used code tags, you can get them by clicking on the # symbol above the text box you posted your message in.

It is always a good idea to include an excerpt from your input file (Between code tags) that contains at the least some of the lines you are trying to match to; presumably the line begining "BEGIN DSSUBRECORD".
# 3  
Old 12-06-2009
try the below:-

Code:
cat awk_1:-

/BEGIN DSSUBRECORD/{c=3;next}c-->0 ; /END DSSUBRECORD/{print}
/Owner/{print}
{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}

---------------------

Code:
a=0
cnt=2
a1="abc.txt"
while [ $a -ne $cnt ]
do
awk -f awk_1 < infile[s] > out_file[s] &
a=$(( $a + 1 ))
done

SmilieSmilieSmilieSmilie
# 4  
Old 12-06-2009
Without optimizing the awk code (which I am sure can be done without pipes) you could us a function:
Code:
dnsfilter() {
  awk '/BEGIN DSSUBRECORD/{c=3;next}c-->0' "$1" |
  awk '/END DSSUBRECORD/{exit}{print}' |
  awk '/Owner/{exit}{print}' |
  awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}'
}
a=0
cnt=2
a1="abc.txt"
while [ $a -ne $cnt ]
do
  printf " "
  dnsfilter $a1
  a=$(( $a + 1 ))
done

# 5  
Old 12-06-2009
Hi,

Thanks for your help. It is working fine.

Code:
dnsfilter() {
  awk '/BEGIN DSSUBRECORD/{c=3;next}c-->0' "$1" |
  awk '/END DSSUBRECORD/{exit}{print}' |
  awk '/Owner/{exit}{print}' |
  awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}'
}
a=0
cnt=2
a1="abc.txt"
while [ $a -ne $cnt ]
do
  printf " "
  dnsfilter $a1
  a=$(( $a + 1 ))
done



I need to pass one more parameter, i tried below passing paramters. It is returning output but not the correct one.

Code:
dnsfilter() {
  awk '/BEGIN DSSUBRECORD/{c=3;next}c-->"$1"' "$2" |
  awk '/END DSSUBRECORD/{exit}{print}' |
  awk '/Owner/{exit}{print}' |
  awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}'
}
a=0
cnt=2
a1="abc.txt"
while [ $a -ne $cnt ]
do
  printf " "
  dnsfilter $a $a1
  a=$(( $a + 1 ))
done



Need help on this.


Thanks

Last edited by Franklin52; 02-02-2010 at 07:24 AM.. Reason: Please use code tags!
# 6  
Old 02-02-2010
awk script and function

I have tried with many options in unix.

Over all information:
The search pattran contains in the file 'N' times. i want to display 'N' times
I need to pass one more parameter, i tried below passing paramters. It is returning output but not the correct one.

Code:
dnsfilter() {
awk '/BEGIN DSSUBRECORD/{c=3;next}c-->"$1"' "$2" |
awk '/END DSSUBRECORD/{exit}{print}' |
awk '/Owner/{exit}{print}' |
awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf"%s,%s,%s\n",n2,n1,n}}'
}
a=0
cnt=2
a1="abc.txt"
while [ $a -ne $cnt ]
do
printf " "
dnsfilter $a $a1
a=$(( $a + 1 ))
done

any help greatly appricated.

Regards,
Suri

Last edited by Franklin52; 02-02-2010 at 07:25 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script not working but command works in command prompt

Hi everyone I have a problem with my script If I try directly this command /usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz It works But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies

2. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

3. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

6. Shell Programming and Scripting

Paste command not working in shell script

Hai , When i use paste command in command prompt its giving expected output but not in the script. Below is the example. $cat file 1 2 3 $cat file1 4 5 6 $paste -d ':' file file1 1:4 2:5 3:6 but when i used the same command in script its giving the output as below : 1 2 3 (3 Replies)
Discussion started by: Subbu123
3 Replies

7. Shell Programming and Scripting

CD command not working in shell script

Hello All, I have tried many permutaion combinations in my shell script but cd command is not working in shell script. Can any one help me out in this. Below is my script. ############ #!/bin/sh set -x on BASE_DIR=/etc/init.d export BASE_DIR cd $BASE_DIR ############# but its... (8 Replies)
Discussion started by: ajaincv
8 Replies

8. Shell Programming and Scripting

help with shell script: cp command not working, but mv command works...

Hello. I would like to ask your help regarding the cp command. We are using a cp command to create a back-up copy of our file but to no avail. It's just not working. We already checked the file and directory permissions and all seems correct. We have a script (ftp.script) which calls on... (1 Reply)
Discussion started by: udelalv
1 Replies

9. Shell Programming and Scripting

"sed" command is not working in shell script

Hi All, I am not much strong in shell scripting... I am using sed command in my script to find and replace a string....... This is how script looks : ############# #!/usr/bin/ksh CONFIG_FILE=iom_test.txt FIND=`echo "NIS_FTP_SERVER1=123.456.iom.com"` REPLACE=`echo... (2 Replies)
Discussion started by: askumarece
2 Replies

10. Shell Programming and Scripting

cd command is not working in my shell script

Hi, Following is my shell script. #!/bin/tcsh view=$1 image=$2 objfld="obj-ppc-$image" echo $view echo $image echo $objfld echo "cleartool setview $view" cleartool setview $view; cd `cd /vob/ios/sys`; In this "cd" is not working and not getting any error. my shell is "tcsh"... (3 Replies)
Discussion started by: amitrajvarma
3 Replies
Login or Register to Ask a Question