Using Awk to Print All Columns from the Nth to the Last

Using awk to print all columns from the nth to the last

Print all columns:

awk '{print $0}' somefile

Print all but the first column:

awk '{$1=""; print $0}' somefile

Print all but the first two columns:

awk '{$1=$2=""; print $0}' somefile

awk to print all columns from the nth to the last with spaces

GNU sed

remove first n fields

sed -r 's/([^ ]+ +){2}//' file

GNU awk 4.0+

awk '{sub("([^"FS"]"FS"){2}","")}1' file

GNU awk <4.0

awk --re-interval '{sub("([^"FS"]"FS"){2}","")}1' file

Incase FS one doesn't work(Eds suggestion)

awk '{sub(/([^ ] ){2}/,"")}1' file

Replace 2 with number of fields you wish to remove

EDIT

Another way(doesn't require re-interval)

awk '{for(i=0;i<2;i++)sub($1"[[:space:]]*","")}1' file

Further edit

As advised by EdMorton it is bad to use fields in sub as they may contain metacharacters so here is an alternative(again!)

awk '{for(i=0;i<2;i++)sub(/[^[:space:]]+[[:space:]]*/,"")}1' file

Output

o p
o p p
o p p p

Awk print all columns and the modified column

You might print the whole line and use split on the 4th column to assemble the values for the 5th column.

awk '
{
split($4,a,":")
print $0, a[1] ":" a[2]-1 "-" a[2]
}
' file

Output

chr1    16597656        16597657        Chr1:16924151 Chr1:16924150-16924151
chr1 149015385 149015386 Chr1:144869084 Chr1:144869083-144869084
chr1 148989775 148989776 Chr1:144894697 Chr1:144894696-144894697

If you field separator is a tab:

awk '
BEGIN {FS=OFS="\t"}
{
split($4,a,":")
print $0, a[1] ":" a[2]-1 "-" a[2]
}
' file

Output

chr1    16597656        16597657        Chr1:16924151   Chr1:16924150-16924151
chr1 149015385 149015386 Chr1:144869084 Chr1:144869083-144869084
chr1 148989775 148989776 Chr1:144894697 Chr1:144894696-144894697

How to print third column to last column?

...or a simpler solution: cut -f 3- INPUTFILE just add the correct delimiter (-d) and you got the same effect.

using awk or sed to print all columns from the n-th to the last

To preserve whitespace in awk, you'll have to use regular expression substitutions or use substrings. As soon as you start modifying individual fields, awk has to recalculate $0 using the defined (or implicit) OFS.

Referencing Tom's sed answer:

awk '{sub(/^([^[:blank:]]+[[:blank:]]+){1}/, "", $0); print}' 1.txt

Only print if the number of field is greater than a value with awk

Addressing OP's question about why the current code outputs 3:

Initially awk doesn't know if $2 is a number or a string.

The sub() call (a string function) tells awk that $2 is to be treated as a string, which also means $2 will be treated as a string for the rest of the script.

This leads to $2 > 20 being treated as a string comparison ('3' > '20') and since '3' (the string) is 'greater than' '20' (the string), a 3 is output.

To facilitate a numeric comparion we need a way to force awk to re-evaluate $2 as a numeric. One method is to add a zero, ie, $2+0. Making this one change to OP's current code:

$ echo "lorem v3" | awk ' { sub("^v","",$2); if ( $2+0 > 20 ) print $2 } '
<<< no output

NOTE: for more details see GNU awk - variable typing


Addressing the latest change to the question:

Sample input:

$ cat input.dat
lorem v3
ipsum v5
text v21
expla v12

Running our awk code (additional print added for clarification) against input.dat:

$ awk ' { print "######",$0; sub("^v","",$2); if ( $2+0 > 20 ) print $2 } ' input.dat
###### lorem v3
###### ipsum v5
###### text v21
21
###### expla v12

print the last letter of each word to make a string using `awk` command

If you have just this one single line to handle you can use

awk '{for (i=1;i<=NF;i++) r = r "" substr($i,length($i))} END{print r}' file

If you have multiple lines in the input:

awk '{r=""; for (i=1;i<=NF;i++) r = r "" substr($i,length($i)); print r}' file

Details:

  • {for (i=1;i<=NF;i++) r = r "" substr($i,length($i)) - iterate over all fields in the current record, i is the field ID, $i is the field value, and all last chars of each field (retrieved with substr($i,length($i))) are appended to r variable
  • END{print r} prints the r variable once awk script finishes processing.
  • In the second solution, r value is cleared upon each line processing start, and its value is printed after processing all fields in the current record.

See the online demo:

#!/bin/bash
s='UDACBG UYAZAM DJSUBU WJKMBC NTCGCH DIDEVO RHWDAS'
awk '{for (i=1;i<=NF;i++) r = r "" substr($i,length($1))} END{print r}' <<< "$s"

Output:

GMUCHOS

Using AWK to compare values in the same column

Like this:

awk 'int($6) && $6 > n{n=$6}END{print n}' file


Related Topics



Leave a reply



Submit