LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-12-2003, 05:02 PM   #1
jsandro7
Member
 
Registered: Nov 2003
Location: Dallas, TEXAS
Distribution: Slackware AMD Athalon XP2200
Posts: 39

Rep: Reputation: 15
Question 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?
 
Old 06-14-2004, 09:59 AM   #2
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
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.
 
Old 06-14-2004, 11:58 AM   #3
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert 1 column into several rows in Linux? markraem Linux - Software 9 03-30-2010 12:24 PM
highlightling certain rows of a datagrid vb.net mrobertson Programming 7 08-11-2005 08:48 AM
columns & rows Ammad Linux - General 1 08-08-2005 05:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 07:47 PM
Hiden Rows in Openoffice inecoiec Linux - Software 0 03-11-2003 07:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration