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 withsubstr($i,length($i))
) are appended tor
variableEND{print r}
prints ther
variable onceawk
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
How to Split One Text File into Multiple *.Txt Files
Kdevtmpfsi Using the Entire Cpu
How to Find Certain Character Position in Shell
Finding Number Is Even/Odd in Assembly
Multiline Bash Command in Jenkins Pipeline
Why Is a Tilde in a Path Not Expanded in a Shell Script
Separately Redirecting and Recombining Stderr/Stdout Without Losing Ordering
Git Asks For Username Every Time I Push
Shell Command to Tar Directory Excluding Certain Files/Folders
How to Kill All Processes With a Given Partial Name
Pipe To/From the Clipboard in a Bash Script
Performing Http Requests With Curl (Using Proxy)
Lost Httpd.Conf File Located Apache
Find and Replace With Sed in Directory and Sub Directories
Appending a Line to a File Only If It Does Not Already Exist
How to Copy Commits from One Git Repo to Another
How to Build & Install Glfw 3 and Use It in a Linux Project
How to Redirect the Output of the Time Command to a File in Linux