Output the 2Nd Column of a File

output the 2nd column of a file

Because the last line of your example data has no first column you'll have to parse it as fixed width columns:

awk 'BEGIN {FIELDWIDTHS = "2 1"} {print $2}'

How to get the second column from command output?

Or use sed & regex.

<some_command> | sed 's/^.* \(".*"$\)/\1/'

Need to print second column of a file in output file

I believe the output of ./samtools view view mybamfile.bam is tab delimited, so the field separator for AWK should be set to tab (-F'\t') in order to get the second column.

Try this command:

 ./samtools view mybamfile.bam | awk -F'\t' '{ print $2 }' > output.txt

Re tab as separator:

If the field separator is not specified AWK will split on "whitepaces" (e.g., space and tab), using -F'\t' makes sure it splits only on tabs, not spaces. This makes a difference in file that contains both spaces and tabs. E.g,

irene adler john moriarty
frank pempleton tim bayliss

assume there is a tab between the two names on each line. If the tab character is not specified as field separator AWK will see 4 fields per line and column 2 will contain adler and pempleton. If tab is specified as field separator, AWK will only see 2 fields on each line, and column 2 will consist of john moriatry and tim baylis.

Tested with GNU Awk 3.1.6 under Linux.

join 2 files based on 1st & 2nd column of file AND 3rd & 4th column of second file

You may use this awk:

awk 'FNR == NR {map[$1,$2] = $3; next} ($3,$4) in map {$NF = map[$3,$4]} 1' f1 f2 | column -t

3 22745180 rs12345 G C
12 67182999 rs78901 A T

A more readable version:

awk '
FNR == NR {
map[$1,$2] = $3
next
}
($3,$4) in map {
$NF = map[$3,$4]
}
1' file1 file2 | column -t

Used column -t for tabular output only.

Print the data from second column

1st solution: Could you please try following based on your shown samples it's written. Written and tested in
https://ideone.com/WoF40j

awk '
BEGIN{
FS="|"
}
{
print $(NF-1)+0
}
' Input_file



2nd solution: Use space and | as a field delimiter one could run following.

awk -F'[[:blank:]]+\\|[[:blank:]]+' '{print $(NF-1)}'  Input_file

OR

awk -F' +\\| +' '{print $(NF-1)}' Input_file

adding columns from one file after a column in a second file while considering the two files have different number of columns


awk -F'\t' -v OFS="\t" '{getline f1 < "file1"; $1 = $1 OFS f1; print}' file2

math on second column of two files

The idiomatic technique is: iterate over the first file, and create a mapping from $1 to $2. Then, iterate over the 2nd file, and use the mapping for the current $1

awk '
NR == FNR { # this condition is true for the lines of the first file [1]
foo[$1] = $2
next
}
{
print $1, (foo[$1] - $2) / sqrt($2)
}
' foo.txt bar.txt

outputs

QGP 10.7947
TGP 2.94732
KGP 1.98107
DGA 22.3034
PGP 3.76599
KGD 13.7153
QGE 9.91737
TGD 9.32047
DGS 18.6388
TGA 10.754

[1]: NR == FNR

FNR is the record number of the current file. NR is the total record number of all files seen so far. Those values will only be the same for the first file.
This breaks down when the first file is empty. In that case, NR == FNR is true for the first file that has at least one line.
A more reliable condition is:

awk '
FILENAME == ARGV[1] {
do stuff for the first file
next
}
{
this action is for each subsequent file
}
' file1 file2 ...


Related Topics



Leave a reply



Submit