Sponsored Content
Top Forums Shell Programming and Scripting AWK commands, in different OS Post 303016041 by uuuunnnn on Wednesday 18th of April 2018 04:55:37 PM
Old 04-18-2018
Display AWK commands, in different OS

I have developed a bash shell script which works perfectly fine when any bash shell is in use except SunOS.

There are two commands that is not compatible.

#!/usr/bin/env bash

Problem
1. The awk command below does not work for SunOS

Requirement

We extract Oracle EBS tables into text files and there is need to calculate the Sum total from the following hashTables for the following hashTableFields.
So GL_BALANCES.txt will have a column PERIOD_NET_CR. We are summing it and storing the result into a Log File.

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

Code:
hashTables=( GL_BALANCES GL_IMPORT_REFERENCES GL_JE_BATCHES GL_JE_HEADERS GL_JE_LINES )
hashTableFields=( PERIOD_NET_CR HASH_IDX HASH_IDX HASH_IDX ACCOUNTED_CR )

# ------------------------------------------------------------------------
# Adding awk command support for SunOS -- use nawk for SunOS
# ------------------------------------------------------------------------
case `uname` in
  SunOS) awkcmd=nawk ;;
  *) awkcmd=awk ;;
esac


for (( index=0; index < $cnt; index++ ))
do
	RIGHT_NOW=$(date +"%x %r %Z")
	printf "%-35s %-20s %-20s\n" "${hashTables[index]}" "Start Time" "${RIGHT_NOW}" >> ${LOG_FILE}
	
	sum=0
	
	"$awkcmd" -F"#|#" -v c="${hashTableFields[index]}" 'NR==1{
		for (i=1; i<=NF; i++) 
		 if ($i==c)
		 {
		   p=i;
		   break
		 }; 
		 next
	  } 
	  {
	   if ( $p=="|" || $p=="#|" || $p=="#" || $p=="|#" )
	    print $(p+1)
	   else
	    print $p
	  }' "${hashTables[index]}.txt" >> temp.hash.txt
	  
	# ----------------------------------------------------------------      
	# Remove blank lines from temp.hash.txt. This should resolve the parser issue if it finds blank values
	# ----------------------------------------------------------------
	sed '/^$/d' temp.hash.txt > temp.hash.out
	mv temp.hash.out temp.hash.txt
	sum=`"$awkcmd" ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc`
				
	# ----------------------------------------------------------------
	# Calling the function to convert hashtotals in correct precision
	# ----------------------------------------------------------------
	sum=`round ${sum} ${precision}`
	if [ $? -eq 0 ]; then
		# Print the hash total
		echo "${hashTables[index]}#|#${hashTableFields[index]}#|#${sum}" >> ${HASHTOTAL_FILE}
	fi
	rm -f temp.hash.txt
	RIGHT_NOW=$(date +"%x %r %Z")
	printf "%-35s %-20s %-20s\n" "${hashTables[index]}" "End Time" "${RIGHT_NOW}" >> ${LOG_FILE}
done



Log file content with Hash total values generated. This is not happening currently in SunOS. For rest OS, it is working as expected
Code:
TABLENAME#|#FIELD#|#TOTAL
GL_BALANCES#|#PERIOD_NET_CR#|#26525675437.9900
GL_IMPORT_REFERENCES#|#HASH_IDX#|#3226845.0000
GL_JE_BATCHES#|#HASH_IDX#|#20623.0000
GL_JE_HEADERS#|#HASH_IDX#|#31556.0000
GL_JE_LINES#|#ACCOUNTED_CR#|#3653881572.0300

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

2. Split command does not work in SunOS

Requirement
If number of records exceed 5000000, then
Code:
GL_BALANCES.txt will be split into

GL_BALANCES_0000.txt
GL_BALANCES_0001.txt

...
...
until all records are placed into multiple files. Each file cannot contain more than 5000000 records. After each file, the subsequent file will contain next set of records.


Code:
splitFiles=( GL_BALANCES GL_IMPORT_REFERENCES GL_JE_BATCHES GL_JE_HEADERS GL_JE_LINES )

for splitFileIdx in "${splitFiles[@]}"
do
	if [[ -f ${splitFileIdx}.txt ]]; then
		RIGHT_NOW=$(date +"%x %r %Z")
		printf "%-35s %-20s %-20s\n" "${splitFileIdx}" "Start Time" "${RIGHT_NOW}" >> ${LOG_FILE}
		
		SPLIT_COUNT=0
		
		tail -n +2 ${splitFileIdx}.txt | split -d -a 4 -l ${row_num_split} - ${splitFileIdx}_
		for file in ${splitFileIdx}_*
		do
			head -n 1 ${splitFileIdx}.txt > tmp_file
			cat $file >> tmp_file
			mv -f tmp_file $file
			mv -f ${file} ${file}.txt
		

			# ****************************************************
			# Get the number of files and check for split success. 
			# if successful delete the original file
			# ****************************************************
			SPLIT_COUNT=$(find ${splitFileIdx}_* -type f | wc -l)
			
			if [[ $SPLIT_COUNT -gt 1 ]]; then
				rm -f ${splitFileIdx}.txt
			else 
				mv -f ${file}.txt ${splitFileIdx}.txt
			fi
		done

		RIGHT_NOW=$(date +"%x %r %Z")
		printf "%-35s %-20s %-20s\n" "${splitFileIdx}" "End Time" "${RIGHT_NOW}" >> ${LOG_FILE}
	fi
done

Please note - the above codes are working as expected in Linux and other OS except SunOS.

We are ready to write them differently based on SunOS compatibility.

Any help is appreciated

Last edited by joeyg; 04-19-2018 at 11:17 AM.. Reason: code tags bolded text removed; better title
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use unix commands in awk

Hi All, I try to use the unix commands inside awk and couldnt succeed . Please help me how to achieve it. Regards, Sukumar. (7 Replies)
Discussion started by: jerome Sukumar
7 Replies

2. Shell Programming and Scripting

Question about awk commands

what does this mean? cat /etc/passwd | awk -F: '{print $5}' | \ awk -F, '{print $1}' | \ awk '{print tolower($l)};{print tolower($2)}' | \ grep -v '^$' >> local_tmp (1 Reply)
Discussion started by: hin-linux
1 Replies

3. Shell Programming and Scripting

How do we use multiple commands with AWK

Hi, I have a scenario where in, I have a file named abc.txt. I extract the file names from it using. awk '/dbf$/{print $NF}' abc.txt /u01/oradata/omc/systab/omcdef.dbf /u01/oradata/omc/oratemp/temptab1.dbf Now I need to further enhance this command and also extract the mount point... (3 Replies)
Discussion started by: geetap
3 Replies

4. UNIX for Dummies Questions & Answers

UNIX commands in AWK

Hi, I have 2 lists of words and I need to take every line in first_list.txt and use agrep command to find similar words in whole second_list.txt. I tried to use AWK, but I dont know how to take current line ($0) and use in inside of agrep cat first_list.txt | awk '' Thanks for help (10 Replies)
Discussion started by: m4rty
10 Replies

5. Shell Programming and Scripting

Using shell commands in awk

Hi, I've searched this site and the wider web and have not found anything (that I can understand..) that helps me. I've used shell commands in awk fine in the past, the difference is that I want to pass the shell command a field variable within awk from the current input. A simple example... (3 Replies)
Discussion started by: Ronnie717
3 Replies

6. Shell Programming and Scripting

awk commands on files

I want to create an awk script that performs different things on different files. For example, for files with extension .ry I might do something, but for .xt I do something else, and another things on .zc files. How can I do this in one awk script? (3 Replies)
Discussion started by: kristinu
3 Replies

7. Shell Programming and Scripting

Help me in awk commands

can you please let me know what does the below awk command does. awk 'NR>2{printf "%-30s %-15s\n", $1, !($4)?$2:$4;}' result.txt (1 Reply)
Discussion started by: ramkumar15
1 Replies

8. UNIX for Dummies Questions & Answers

How to use shell commands within awk?

Hey guys i want to use shell commands like ls, find, cd and more with in awk statements with inputs from the awk variables. Like in the below code how can i change the directory using the value of path. Please suggest awk '{ while (i<NR) { i++; percentage = $5; path = $6; ... (2 Replies)
Discussion started by: rishi90
2 Replies

9. Shell Programming and Scripting

Combine awk commands into one

my code: gawk 'NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"'' ${LOGFILE} | gawk '{l=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o=i; t++;} END { for(i=1; i<=NR; i++) if (o) print l; print t+=0;}' i would like to combine this into one... (5 Replies)
Discussion started by: SkySmart
5 Replies

10. Shell Programming and Scripting

Combine two awk commands

Hi, Can someone please guide me how to combine the following two awk calls in one? I noticed that it is very often situation for me, and I think that it can be replaced with one awk call. The question is more general, not the exact one. echo "A B C/D" | awk '{print $3}' | awk -F/ '{print... (4 Replies)
Discussion started by: mirusnet
4 Replies
All times are GMT -4. The time now is 03:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy