9.16. Printing other information from a file

Print the number of lines in file3.csv and file4.txt.

file3.csv file4.csv

$ awk '{print NF}' file3.csv | wc -l
28

$ nlines=$(awk '{print NF}' file3.csv | wc -l)
$ echo $nlines
28

$ awk '{print NF}' file4.txt | wc -l
29

$ nlines=$(awk '{print NF}' file4.txt | wc -l)
$ echo $nlines
29

Print the number of columns in file3.csv and file4.txt.

Comma-separated file Space-separated file

$ awk -F',' '{print NF}' file3.csv | sort –nu
3

$ ncols=$(awk -F',' '{print NF}' file3.csv | sort -nu)
$ echo $ncols
3

$ awk '{print NF}' file4.txt | sort -nu
3

$ ncols=$(awk '{print NF}' file4.txt | sort -nu)
$ echo $ncols
3

Print the length of each line of file4.txt.

To get the length of a string you can use the function length(), and pass as parameter $0 which obtains all the fields (the whole line).

Print each line Print the length of each line
$ awk '{print $0}' file4.txt
AnonymizedID SubjectGroup AGE
B11108326 Group1 59
B11108399 Group1 23
B11110893 Group1 28
B11119909 Group2 61
D11144030 Group3 11
D11144030 Group3 13
B11119903 Group2 84
C11131039 Group2 67
C11133100 Group1 23
C11135566 Group2 72
C11137159 Group3 11
C11137159 Group3 12
C11137167 Group3 14
C11137167 Group3 16
C11137439 Group3 15
C11137439 Group3 79
C11137443 Group3 15
C11137544 Group1 22
C11137123 Group2 68
C11138150 Group1 44
C11138152 Group1 10
C11138797 Group1 24
C11138184 Group1 57
C11138122 Group1 23
C11138122 MISSING 25
C11138192 Group1 45
B12226507 Group1 26
B12226546 Group1 55
$ awk '{print length($0)}' file4.txt
29
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
20
19
19
19

Print the length of the second field (length($2)) in file3.csv and file4.txt.

Comma-separated file Space-separated file

$ awk '{print length($2)}' file4.txt
12
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
7
6
6
6

$ awk -F',' '{print length($2)}' file3.csv
13
7
6
6
6
6
6
6
6
6
6
6
6
6
8
8
8
6
6
8
6
6
6
6
6
6
8
6

Print all the lines of file4.txt in upper-case.
To convert a string to upper-case use the function toupper() and pass as parameter $0 which contains the whole line.

$ awk '{print toupper($0)}' file4.txt
ANONYMIZEDID SUBJECTGROUP AGE
B11108326 GROUP1 59
B11108399 GROUP1 23
B11110893 GROUP1 28
B11119909 GROUP2 61
D11144030 GROUP3 11
D11144030 GROUP3 13
B11119903 GROUP2 84
C11131039 GROUP2 67
C11133100 GROUP1 23
C11135566 GROUP2 72
C11137159 GROUP3 11
C11137159 GROUP3 12
C11137167 GROUP3 14
C11137167 GROUP3 16
C11137439 GROUP3 15
C11137439 GROUP3 79
C11137443 GROUP3 15
C11137544 GROUP1 22
C11137123 GROUP2 68
C11138150 GROUP1 44
C11138152 GROUP1 10
C11138797 GROUP1 24
C11138184 GROUP1 57
C11138122 GROUP1 23
C11138122 MISSING 25
C11138192 GROUP1 45
B12226507 GROUP1 26
B12226546 GROUP1 55

Print all the lines of file4.txt in lower-case.
To convert a string to lower-case use the function tolower() and pass as parameter $0 which contains the whole line.

$ awk '{print tolower($0)}' file4.txt
anonymizedid subjectgroup age
b11108326 group1 59
b11108399 group1 23
b11110893 group1 28
b11119909 group2 61
d11144030 group3 11
d11144030 group3 13
b11119903 group2 84
c11131039 group2 67
c11133100 group1 23
c11135566 group2 72
c11137159 group3 11
c11137159 group3 12
c11137167 group3 14
c11137167 group3 16
c11137439 group3 15
c11137439 group3 79
c11137443 group3 15
c11137544 group1 22
c11137123 group2 68
c11138150 group1 44
c11138152 group1 10
c11138797 group1 24
c11138184 group1 57
c11138122 group1 23
c11138122 missing 25
c11138192 group1 45
b12226507 group1 26
b12226546 group1 55

Some other functions that can be used in addition to toupper() and tolower() can be found here