Variable and awk inside for loop


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Variable and awk inside for loop
# 1  
Old 12-06-2017
Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue.

Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present inside the file. And, I am passing the file name at run time.

This is the file am passing at run time.

Code:
COLUMN_A|COLUMN_B|COLUMN_C
001|AAAAAA|YUYWD
002|ABCOHFOHNFN|DLDLULDNSFHCLYHNDJK
003|BCXSOHNSN|XCDFHNSH
004|ZBDIWUFHOJKSJF|DGFTSVVCD
005|ZXSEDTRWEF|XCDFHNSHDGFTSVVCD

This is what I have done so far:
Code:
nbrofcols=`head -1 $1 | awk -F'|' '{print NF;}'`
cat /dev/null > nbrofcols.txt
for ((i = 1; i <= $nbrofcols; i++));
do
  awk 'NR>1' $1 | awk -F"|" ' length("$i") > len { len=length("$i") } END { print len } ';
done >> nbrofcols.txt
:

Expected Output
Code:
COLUMN_A: 3
COLUMN_B: 14
COLUMN_C: 19

Note: Awk works on a file as a stand alone but not able to get it working in for loop.

Any help would be greatly appreciated.

Last edited by Scott; 12-06-2017 at 05:54 PM.. Reason: Added code tags to data
# 2  
Old 12-06-2017
You don't really need a variable to store number of columns. You can use the built-in variable NF instead:-
Code:
awk -F\| '
        NR == 1 {
                for ( i = 1; i <= NF; i++ )
                        hdr_col[i] = $i
        }
        NR > 1 {
                for ( i = 1; i <= NF; i++ )
                        max_col[i] = ( length($i) > max_col[i] ? length($i) : max_col[i] )
        }
        END {
                for ( i = 1; i <= NF; i++ )
                        print hdr_col[i] ":" max_col[i]
        }
' filename

# 3  
Old 12-06-2017
Thanks Yoda for such a quick response.

However, I am getting syntax error in line # 8. Also, could you please explain line # 8?

Much appreciated!
# 4  
Old 12-06-2017
What is the error??

(also, if you're using Solaris, use nawk or /usr/xpg4/bin/awk)
# 5  
Old 12-06-2017
This is the error I am getting.

Code:
awk: syntax error near line 8
awk: illegal statement near line 8


Last edited by Scott; 12-06-2017 at 07:53 PM.. Reason: Code tags, please...
# 6  
Old 12-06-2017
And the OS you're using?
# 7  
Old 12-06-2017
Code:
SunOS apiaifodb01 5.10 Generic_150400-52 sun4u sparc SUNW,SPARC-Enterprise

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. UNIX for Beginners Questions & Answers

Variable inside while loop got reset

hi, I am using hp unix server and not getting variable output present inside the while loop. I have tried changing the code and need to verify whether it is proper practice of code. I am expecting the output of varible RUN_FILE 3 to TRUE which i get inside the while loop. RUN_FILE 1=TRUE... (8 Replies)
Discussion started by: gowthamsoft
8 Replies

3. Shell Programming and Scripting

awk appending values inside a for loop

Hi i have a 2 files say test1 and test2 with the following data. cat file test test1 i want to append the output from a awk one liner to both the files. for i in cat file;do awk '/ whats happening is its printing the output properly. but not appending the way it showing in print... (1 Reply)
Discussion started by: venkitesh
1 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

Automatic variable assignment inside a for loop

cs1=`echo "scale=8;($css1/$css0)*100"|bc` cs2=`echo "scale=8;($css2/$css0)*100"|bc` cs3=`echo "scale=8;($css3/$css0)*100"|bc` cs4=`echo "scale=8;($css4/$css0)*100"|bc` cs5=`echo "scale=8;($css5/$css0)*100"|bc` cs6=`echo "scale=8;($css6/$css0)*100"|bc` cs7=`echo "scale=8;($css7/$css0)*100"|bc`... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

7. Shell Programming and Scripting

AWK inside For loop

Hi, awk -F"|" 'BEGIN{sum=0}{sum+=$2}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$3}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$4}END{printf("%d\n", sum)}' css.txt awk -F"|" 'BEGIN{sum=0}{sum+=$5}END{printf("%d\n", sum)}' css.txt awk -F"|"... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

8. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

9. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

10. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question