Expressing tail through a variable

277 views Asked by At

So I have a chunk of formatted text, I basically need to use awk to get certain columns out of it. The first thing I did was get rid of the first 10 lines (the header information, irrelevant to the info I need).

Next I got the tail by taking the total lines in the file minus 10. Here's some code:

   import=$HOME/$1

if [ -f "$import" ]
 then
        #file exists
        echo "File Exists."
        totalLines=`wc -l < $import`

        linesMinus=`expr $totalLines - 10`
        tail -n $linesMinus $import
        headless=`tail -n $linesMinus $import`






else
        #file does not exist
        echo "File does not exist."
fi

Now I need to save this tail into a variable (or maybe even separate file) so I can access the columns.

The problem comes here:

headless=`tail -n $linesMinus $import`

When I save the code into this variable and then try to echo it back out, it's all unformatted and I can't distinguish columns to use awk on.

How can I save the tail of this file without compromising the formatting?

2

There are 2 answers

2
Luke Danger Kozorosky On

Figured it out, I kind of answered my own question when I asked it. All I did was redirect the tail command to a file in the home directory and I can cat that file. Gotta remember to delete it at the end though!

3
Greg A. Woods On

Just use Awk. It can do everything you need all at once and all in one program.

E.g. to skip the first 10 lines, then print the second, third, and fourth columns separated by spaces for all remaining lines from INPUT_FILE:

awk 'NR <= 10 {next;}
     {print $2 " " $3 " " $4;}' INPUT_FILE