I have this code below that only prints out certain columns from the first two rows (doesn't affect rows 3 and beyond). How can I do the same on a partial header pattern �G_TP� instead of having to know specific column numbers (e.g. 374-479)? I've tried many other commands within this pipe with no luck.
I have 53 columns out of 600 or so that the header pattern begins with �G_TP�, but the columns may change positions and if that happens, my method below won't work any longer because of the fixed column number �374-479�.
Here are the full input (..fsw1952) and output (..fsw1953) files attached. As you can see, after I run the code from my initial post, the output file (..fsw1953) only contains the header pattern "G_TP" columns in the first two rows only (header and data).
This is the correct output, but I just want my code to perform this based on partial pattern instead of fixed column numbers (as mentioned before).
#!/bin/ksh
ERE=${1:-"^G_TP"}
file=${2:-"f16_may_25_16_23_04_fsw1952.txt"}
awk -F, -v ERE="$ERE" '
NR == 1 {
for(i = 1; i <= NF; i++)
if($i ~ ERE)
of[++nof] = i
}
NR < 3 {for(i = 1; i <= nof; i++)
printf("%s%s", $of, (i == nof) ? "\n" : FS)
next
}
1' "$file"
Although written and tested using a Korn shell, this should work with any shell supporting POSIX shell syntax. If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .
When this script is run with no operands, it produces output identical to the file you attached named f16_may_25_16_23_04_fsw1953.txt .