unix - count of columns in file
awk -F'|' '{print NF; exit}' stores.dat
Just quit right after the first line.
How do I count the number of rows and columns in a file using bash?
Columns: awk '{print NF}' file | sort -nu | tail -n 1
Use head -n 1
for lowest column count, tail -n 1
for highest column count.
Rows: cat file | wc -l
or wc -l < file
for the UUOC crowd.
Count the number of rows per column in Bash Shell
Based on assumption of required output, you have given
$ cat testfile
ID Name
------------
13 Sara
22 Suzan
null Mark
49 John
$ awk '$1 ~ /^[0-9]*$/{ count++ }END{print count}' testfile
3
$ awk 'function is_num(x){return(x==x+0);} is_num($1){ count++ }END{print count}' testfile
3
awk to find number of columns for each line and exit if more than required
Sure you can do:
awk -F, 'NF > 1{exit} 1' file
This will give output as:
ME
TEST
HELLO
WORLD
as NF>1
condition exits the awk as soon as there are more than 1 columns.
EDIT: As per comments below OP wants to print first row with 2 columns and exit. This command should work:
awk -F, 'NF > 1{print; exit}' file
BOO,HOO
How to count the number of columns in the header of a csv file?
Just print NF
and exit.
awk -F, '{print NF; exit}' file.csv
It should be pointed out that this is not safe for arbitrary csv as csv can quote commas within elements and awk will not handle that correctly.
How to add new column based on values of existing column in .txt file unix
Using awk
:
awk 'NR==1{$7="V8";print;next}\
$6 == "Yes" {$7="2"};\
$6 == "No" {$7="1"}1' a.txt |column -t
- The first line print the
V8
in the header - If the value of 6th col:
V7_Pheno
isYes
, then$7 = "2"
- If the value of 6th col:
V7_Pheno
isNo
, then$7 = "1"
column -t
: for better formatting
Related Topics
Insert New Line to Bash Prompts
About the Memory Layout of Programs in Linux
How to Execute an Local Script in Remote Server with Parameters
Distinguish .Shstrtab and .Strtab in Elf File
Command Not Found in Bash's If-Else Condition When Using [! -D "$Dir"]
Should %Rsp Be Aligned to 16-Byte Boundary Before Calling a Function in Nasm
Pyqt5 Error "Pycapsule_Getpointer Called with Incorrect Name"
Why Does the Wc Command Count One More Character Than Expected
Add Blank Line Between Lines from Different Groups
Linux: Writes Are Split into 512K Chunks
Pipe Bash Command Output to Stdout and to a Variable
Makefile with Multiple Targets
How to Set a Variable Used in a Perl Script as Environment Variable
How to Make One Linux Kernel Module Depend on Another External Module with Depmod