PDA

View Full Version : AWK And Bash Functions


soup4you2
May 30th, 2003, 23:55
Ok here's my problem... i'm working on a script to make my work a lot easier... so i've simplified it a TON by writting 2 little files... both can be seen below.. but i'm trying to figure out how to make the AWK file a actual function in the bash script... any help would be gladly appeiciated...

---=[STX PROCESSING SCRIPT]=---

[code:1:b939c89cd1]
################################################## #
# STX Parser
# Written By: soup4you2
# Website: http://bsdhound.com
# Last Update: 30 MAY 03
################################################## #
clear
echo " "
echo " ---=[ Welcome To Soup's STX Parsing Utility ]=---"
echo " "
echo "Welcome $USER,"
echo " "

function stxname {
echo " "
echo "STX Files in Current Directory Are:"
echo " "
ls -al | grep STX
echo " "
echo -n "What is the name of the STX filename? >"
read STX
echo " "
echo -n "You have entered in $STX, Is this correct? [Y/N] >"
read yesnoanswer
case $yesnoanswer in
y* | Y*)
pu=1
;;
n* | n*)

# NOT CORRECT
echo " "
stxname
exit 0
;;
esac

#CORRECT
echo " "
echo "Thanks...."
echo " "
parse_1
}

function parse_1 {
echo "Begining Parsing Articles and Titles.."
echo " "
cat $STX | egrep -B1 -A1 'Article_title|Paragraph_1' > $TMP/parse_1.out
cat $TMP/parse_1.out|sed 's/TEXT//g'|sed 's/{//g'| \
sed 's/}//g'|sed 's/,//g'|awk 'BEGIN{FS="\n";OFS="\t";} \
{print $1,$2}' >> $TMP/parser_1.output
}

stxname
awk -f stx.awk $TMP/parser_1.output >> $STX.parsed

echo " "
echo "Cleaning Up Working Files"
rm $TMP/parse_1.out
rm $TMP/parser_1.output
echo "Done..."
echo "File Saved As $STX.parsed"
echo " "
echo -n "Would you like to view this file now? [Y/N] >"
read yesnoanswer
case $yesnoanswer in
y* | Y*)
vw=1
;;
n* | N*)

#NO
exit 0
;;
esac

#yes
cat $STX.parsed
[/code:1:b939c89cd1]

Thats the bash script... and here's the stx.awk file

---=[ STX.AWK ]=---

[code:1:b939c89cd1]
BEGIN {OFS = "\t";print "\n\n============================================= ======================";print "STX Parser Written By: Soup "}
/Article_title/ {ORS = "\n";print"======================================= ============================"}
/Article_title/ {
print $0;
getline;
print $0;
}
/Paragraph/
[/code:1:b939c89cd1]

any help would be appriciated....

frisco
May 31st, 2003, 01:27
Maybe i'm missing something (it's 11pm and i'm in a hurry to get out the door), but can't you place the awk script in the bash script, wrapped in single quotes? For example:
[code:1:e58c273170]
inti$ ls
myawk mysh
inti$ cat myawk
BEGIN {
print "files:"
}
{
print $1
}

inti$ cat mysh
#!/bin/sh

ls | awk -f myawk

ls | awk '
BEGIN {
print "files:"
}
{
print $1
}
'

inti$ ./mysh
files:
myawk
mysh
files:
myawk
mysh
[/code:1:e58c273170]