--SunOS 5.10 nawk for paragraph not working

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting --SunOS 5.10 nawk for paragraph not working
# 1  
Old 07-04-2019
--SunOS 5.10 nawk for paragraph not working

The machine is using bash:
 bash -version
GNU bash, version 3.2.51(1)-release (i386-pc-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

I have the following xml file. am trying to get a whole paragraph if it meets certain criteria.
In this case , just a date.
XML File
<timestamp>2019-07-03 09:45:08</timestamp>
<message>ERROR on transfer etranfer_in_emtc: Process Download file EMTC%m%d.DAT failed
 due to FileNotFoundException: pas de fichier EMTC0703.DAT</message>
<timestamp>2019-07-03 10:45:09</timestamp>
<message>ERROR on transfer etranfer_in_emtc: Process Download file EMTC%m%d.DAT failed
 due to FileNotFoundException: pas de fichier EMTC0703.DAT</message>
<timestamp>2019-07-03 12:45:54</timestamp>

The command:
CURR_DATE=$(TZ=GMT+24 date +%Y-%m-%d)
 echo $CURR_DATE

nawk -v var="$CURR_DATE" 'BEGIN{RS="\<run\>"}; $0 ~ var {print $0}' $XML_FILE


This command on linux gives the whole paragraph starting with the <timestamp>
to the </run> line.

But on Solaris , i only get the <timestamp> line.

 nawk -v var="$CURR_DATE" 'BEGIN{RS="\<run\>"}; $0 ~ var {print $0}' $XML_FILE
timestamp>2019-07-03 09:05:07
timestamp>2019-07-03 09:15:06
timestamp>2019-07-03 09:45:08
timestamp>2019-07-03 10:45:09
timestamp>2019-07-03 12:45:54


Any ideas ?? Thank you

Last edited by vgersh99; 07-04-2019 at 09:55 AM.. Reason: Code tags, please!
# 2  
Old 07-04-2019
nawk on Solaris doesn't support regex or non-single char as RS. In other words, only single chars for RS.

You can try your attempt with Solaris' /usr/xpg4/bin/awk
The alternative (not tested as I don't have Solaris):
nawk -v d='2019-07-03' '/<run>/,/<\/run>/ && $0~d' myXML.xml

Last edited by vgersh99; 07-04-2019 at 10:31 AM..
# 3  
Old 07-04-2019
Thank you for the prompt reply.
But all attempts using awk/nawk failed on Solaris.

Had to use the following perl code to get it working:

export CURR_DATE=$(TZ=GMT+24 date +%Y-%m-%d)
perl -ne 'print if /$ENV{CURR_DATE}/ .. /\<run/' $XML_FILE

# 4  
Old 07-04-2019
You can tell the shell to insert the variable into the perl code string:
perl -ne 'print if /'"$CURR_DATE"'/ .. /\<run/' $XML_FILE

It is a simple concatenation 'string'"string"'string'.
Also doable with sed
sed -n '/<run>/,/<\/run>/H; /<run>/h; /<\/run>/{x;/'"$CURR_DATE"'/p;}' $XML_FILE

Not so nice: the search REs are named more than once.
Here comes a portable awk solution:
awk -v d="$CURR_DATE" '{ buf=(buf RS $0) } /<\/run>/ && buf ~ d { print buf } /<run>/ { buf=$0 }' $XML_FILE

Interesting variant: omit the RS delimiter.
# 5  
Old 07-04-2019
Thank you.

Unfortunately, the sed/awk commnands that work on other platforms
do not work on Solaris.

At least in the environments I work in.

Hence the work-arounds i had to use.
# 6  
Old 07-04-2019
Have you tried them? I have made them portable.
Of course in Solaris you must use nawk or /usr/xpg4/bin/awk - the /bin/awk is not compliant to any standard.

My awk code translated to bash builtins:
while IFS= read line; do buf+=$'\n'$line; case $line in ("<run>") buf=$line;; ("</run>") [[ $buf == *"$CURR_DATE"* ]] && echo "$buf";; esac; done < $XML_FILE

# 7  
Old 07-05-2019
Hi gigamesh,
The following should work with either nawk or /usr/xpg4/bin/awk on Solaris 10:
CURR_DATE=$(TZ=GMT48 date '+%Y-%m-%d')
/usr/xpg4/bin/awk -v var="$CURR_DATE" '
/<run>/ {
	found = 0
	record = $0
{	record = record "\n" $0
$0 ~ var {
	found = 1
/<\/run>/ && found {
	print record
}' file.XML

Note that I used TZ=GMT48 because your sample data only contains July 3rd datestamps and it is no longer July 4th in Greenwich in GMT and I used a hard-coded pathname for your sample data file (where I placed it on my system) because your sample code didn't initialize the XML_FILE variable.

I don't see any reason why the awk code MadeInGermany suggested should not work (assuming that you defined CURR_DATE and XML_FILE appropriately when you tried running it). In what way did it fail?

Hi MadeInGermany,
The awk in /bin or /usr/bin on Solaris 10 systems is the original awk produced by Aho, Weinberg, and Kernighan in the 1970's. There are still some scripts laying around that depend on that version of awk.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Sendmail not working in SUNOS

Hi All i am trying to send a mail from SunOS to my outlook mail but it is not working neither giving any error please suggest # uuencode /tmp/t.txt t.txt | mailx -s test v.com # # uname -a SunOS 5.9 Generic_122300-25 sun4u sparc SUNW,Sun-Fire-V440 (3 Replies)
Discussion started by: scriptor
3 Replies

2. UNIX for Advanced & Expert Users

Manipulate files with find and fuser not working as expected on SunOs

Greetings, For housekeeping, I use the following command: find /some/path -type f -name "*log*" ! -exec fuser -s "{}" 2>/dev/null \; -exec ls -lh {} \; It finds all log files not currently in use by a process and manipulates them. This command always works on linux and redhat machines,... (2 Replies)
Discussion started by: dampio
2 Replies

3. Shell Programming and Scripting

Nawk command not working for Question mark (?)

Hi Folks, I am facing an issue with nawk command. The data is as below: ABC0022,BASC,Scene Package,INR,02May17,XXX4266,be?. Hotel,3,AW01,Twin Room,61272,41308,39590,39590,X,X ABC0022,BASC,Scene Package,INR,02May17,XXX4266,be?. Hotel,3,AW02,Twin Room with Balcony,9272,85638,4520,9590,X,X... (1 Reply)
Discussion started by: kirans.229
1 Replies

4. Shell Programming and Scripting

mailx not working on SunOS 5.9

I m trying to send o/p of one file using mailx command but is not working PFB command : cat healthchecklog | mailx -s "HEALTH CHECKS" abc@jkl.com also I have checked the ps -ef for mailx which is giveing below o/p ps -ef | grep mail root 364 1 0 Jun 08 ? ... (11 Replies)
Discussion started by: Jcpratap
11 Replies

5. Solaris

SunOs 5.9: why is 'ulimit -c' not working?

I have a situation where the system is dumping a 2g causing filesystem to fill up. We identified the source and working on a solution. However, I wanted to limit the size of the 'core' file. Please examine the test scenario ... cnewtonne@mars> ulimit -f 0 cnewtonne@mars> ls -ltr core*... (2 Replies)
Discussion started by: cnewtonne
2 Replies

6. Solaris

All i/o (ftp) not working on this SunOS 5.10

Hi everyone: I have been trying to simply FTP a file over to this SunOS 5.10 (which is same as Solaris 10 now) and nothing works! I can only Telnet to this via SSH (with my own account/uid), and I can also send simple mail out with mailx, that's all!!! I cannot FTP to it (I think FTP... (7 Replies)
Discussion started by: steve701
7 Replies

7. Shell Programming and Scripting

Nawk Script not working

Any idea why this isn't working? YESTERF=`TZ=aaa24 date +%b"-"%d | sed 's/-0/--/'` filelist2=$(find /export/home/gen/cks/traces \( -name \*YESTERF\* -name \*DNA\* \) -print | tr '\n' ' ') print "Date/Time,Location,Shelf,IP,Reason,Log Filename" >> $OUTPUT nawk -F':' ' $2 ~... (2 Replies)
Discussion started by: ther2000
2 Replies

8. Solaris

tr -d is not working on SunOS 5.9

i am using sunos 5.9. entityname="india\/delhi" correctpattern="<branch value=\"/`echo $entityname | tr -d '\'`/WORKAREA/\"> echo $correctpattern the output should be <branch value="/india/delhi/WORKAREA/"> This is working fine in command line but not working when i placed these... (1 Reply)
Discussion started by: millan
1 Replies

9. UNIX for Dummies Questions & Answers

SunOS 5.10 - VI Arrow keys not working

Hi I am working on SunOS 5.10 from remote terminal using putty. Also echo $TERM xterm In vi editor when in insert mode arrow keys are not working for cursor movement instead they print A B C and D. Please help. thanks ravs (7 Replies)
Discussion started by: ravashingravi
7 Replies

10. Shell Programming and Scripting

NAWK Script not working fine

Hello to all can any one help me out with a nawk script. Actually i am having a shell script which uses nawk pattern searching and it is not parsing the file properly. I have been debugging it since long time, but nt able 2 find the root cause.. If any one can help me out with this one .. (3 Replies)
Discussion started by: dheeraj19584
3 Replies
Login or Register to Ask a Question

Featured Tech Videos