LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I there a way to tell awk to select rows instead of collums (https://www.linuxquestions.org/questions/linux-newbie-8/i-there-a-way-to-tell-awk-to-select-rows-instead-of-collums-125312/)

jsandro7 12-12-2003 04:02 PM

I there a way to tell awk to select rows instead of collums
 
Hey,
I am trying to get awk to display multi lines (rows)
I know that $1 will select the column one but how do I tell the awk to select multi rows like to print lines 4-8.
Is there a way to do that?
Or do you have to use sed?

arobic 06-14-2004 08:59 AM

You can simply use the built-in variable for row number in awk, NR, in a script like this:

#!/bin/sh
touch newfile
for i in 4 5 6 7 8
do
awk -v a=$i 'NR==a {print $0}' file >> newfile
done


This script will do a loop on the lines you want to print (here 4 5 6 7 8), tell awk to associate the shell variable i to its own variable a and print the complete line ($0) of file into newfile. The -v option allows you to give values to awk's variable. The touch statement creates the newfile and the redirection symbol >> append lines into the newfile instead of overwriting them.

stickman 06-14-2004 10:58 AM

Adapt to suit your needs:
awk '(NR > 3 && NR < 9) {print $0}' file


All times are GMT -5. The time now is 07:41 PM.