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 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
How to print the value in third column of a line which comes after a line which, contains a specific string using AWK to a different file?
This would not be an appropriate job for getline, see http://awk.freeshell.org/AllAboutGetline, and idk why you're setting FS to :
with -F:
when your fields are space-separated as awk assumes by default.
Here's how to do what I think you're trying to do with 1 call to awk:
awk 'f{print $3; f=0} /Energy/{f=1}' "$main_path/surf_"*"/inputfile > outputfile
How to select the last column of dataframe
Use iloc and select all rows (:
) against the last column (-1
):
df.iloc[:,-1:]
AWK print every other column, starting from the last column (and next to last column) for N interations (print from right to left)
There are certainly more elegant ways to do this, but I am not really an awk
person:
Part 1:
awk -F@ '{ x = ""; for (f = NF; f > (NF - 5 * 2); f -= 2) { x = x ? $f "@" x : $f ; } print x }' file.csv
Output:
2@2.25@1.5@1@3.25
9@5.25@4@3@3.25
4@.2@.5@1@3.75
8@13.75@13@8.5@6
1@.2@.5@3@6.5
7@10.25@10.5@11@12.75
Part 2:
awk -F@ '{ x = ""; for (f = NF - 1; f > (NF - 5 * 2); f -= 2) { x = x ? $f "@" x : $f ; } print x }' file.csv
Output:
SayWhat@4@3@1@1
Smarty@6@5@4@2
IfYouLike@1@1@2@3
LaughingHard@8@8@6@4
AtFunny@3@2@3@5
PunchLines@7@7@8@6
The literal 5
in each of those is your "number of iterations."
Print second-to-last column/field in `awk`
awk '{print $(NF-1)}'
Should work
How to pick last column from data and print sum of it?
use warnings;
use strict;
use feature 'say';
my $tot = 0;
my $header = <>;
while (<>) {
$tot += (split /,/)[-1];
}
say join ',', 'FINAL', $.-1, $tot;
The <>
operator reads lines of files submitted at command-line at invocation. The $.
variable is the current line number of the last-used filehandle.
Formatting of numbers (and the number of their decimal places) is nicely down with printf
.
The above processes data from a file. However, the last line, with FINAL...
, that is shown in the question is indeed the last line of the input file, which need be checked. Then that also shouldn't be processed so we need to add a check
while (<>) {
if (/^\s*FINAL/) {
my @fields = split /,/;
if ($.-1 != $fields[1] or $tot != $fields[-1]) {
warn "Wrong FINAL line: $_";
}
last;
}
$tot += (split /,/)[-1];
}
But then we process that line multiple (three) times. It is nicer, and also certainly more amenable to checking input data, to first split
each line into an array and use the ready array
while (<>) {
my @fields = split /,/;
# Is this the last line, with a summary to be checked?
if ($fields[0] eq 'FINAL') {
if ($.-1 != $fields[1] or $tot != $fields[-1]) {
warn "Wrong FINAL line: $_";
}
last;
}
# Validate input if/as needed
$tot += $fields[-1];
}
This added array construction reflects visibly (and negatively) on the efficiency only if the file is rather large, or many such files are processed. But if input data indeed need be checked for format, validity, or pre-processed in any way, then of course there is no choice. (In very short files this may be more efficient but one would be hard pressed to detect that since it's about only one line.)
Another option, if the file isn't too large, is to read all lines into an array first, peel off the unneeded first line (header) and the last summary line, and process the rest
my @lines = <>;
chomp @lines;
my $header = shift @lines;
my $final = pop @lines;
foreach my $line (@lines) {
# or split into an array for checking of various fields etc
$tot += (split /,/, $line)[-1];
}
say join ',', 'FINAL', scalar @lines, $tot;
CHECK_FINAL_LINE: {
my @fields = split /,/, $final;
if ( $fields[1] != @lines or $fields[-1] != $tot ) {
warn "FINAL summary line wrong: $final";
}
};
Now we avoid checking each and every line for whether it is the last.
The number of data lines is the length of the array @lines
, produced when @lines
array is used in scalar context, like in the if
in the last block. I put the check in a block so to avoid introducing a @fields
array for the rest of the program (this way it's scoped to that block only), and I name the block CHECK_FINAL_LINE
for convenience/clarity. The block isn't necessary.
In the line where the calculated final is printed though we have a list context, imposed by print
(and so by say
), and we actually need an explicit scalar
.
How to print last two columns using awk
You can make use of variable NF
which is set to the total number of fields in the input record:
awk '{print $(NF-1),"\t",$NF}' file
this assumes that you have at least 2 fields.
Sort according to two columns and extract top two based on last column
Piping the current sort
results to awk
:
$ sort -nk2 -nk3 file.list | awk 'a[$2]++ < 2'
run2/xx2/x2c2.txt 18 -192
run3/xx2/x2c4.txt 18 -184
run3/xx2/x2c2.txt 19 -192
run2/xx2/x2c3.txt 19 -191
run3/xx2/x2c3.txt 21 -191
run1/xx2/x2c1.txt 21 -190
Where:
- field #2 (
$2
) is used as the index for arraya[]
- if the value stored in the array is less than 2 then print the current input line
- then increment the counter (
++
) - 1st time we see
a[18]
the count is 0, we print the line, and increment the count by 1 - 2nd time we see
a[18]
the count is 1, we print the line, and increment the count by 1 - 3rd (to nth) time we see
a[18]
the count is greater than or equal to 2, we do not print the line, and increment the count
An alternative where we increment the count first:
$ sort -nk2 -nk3 file.list | awk '++a[$2] <= 2'
run2/xx2/x2c2.txt 18 -192
run3/xx2/x2c4.txt 18 -184
run3/xx2/x2c2.txt 19 -192
run2/xx2/x2c3.txt 19 -191
run3/xx2/x2c3.txt 21 -191
run1/xx2/x2c1.txt 21 -190
How to move first column to last column in unix?
Perl is handy for text manipulation:
perl -lane 'push @F, shift @F; print "@F"' file
Related Topics
Format and Then Convert Txt to CSV Using Shell Script and Awk
Convert String to Hexadecimal on Command Line
When Should I Wrap Quotes Around a Shell Variable
32-Bit Absolute Addresses No Longer Allowed in X86-64 Linux
Facing an Error "*** Glibc Detected *** Free(): Invalid Next Size (Fast)"
How to Permanently Set $Path on Linux/Unix
Where Is the X86-64 System V Abi Documented
How to Exclude a Directory When Using 'Find'
Maximum Number of Threads Per Process in Linux
How to Show All Shared Libraries Used by Executables in Linux
How to Setup & Run Phantomjs on Ubuntu
How to Insert a Text At the Beginning of a File
Run an Untrusted C Program in a Sandbox in Linux That Prevents It from Opening Files, Forking, etc.