Solved

Grep a single line out of file?

Posted on 2000-05-16
3
212 Views
Last Modified: 2008-02-26
I need a shell script that will return a single value from a file given a particular line.  For example a file contains:

10
20
30
33
44

I need a command that can return the value 33 when passed in the param 4, for line 4.

Thanks,
Ethan
0
Comment
Question by:epost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
geotiger earned 50 total points
ID: 2814680
One liner will do:

cat yourfile | awk '{if (NR==4) print $0}'

This will print the fourth line. If you want just print particular field in the line. Please let me know.
0
 

Author Comment

by:epost
ID: 2815180
Cool, how can I turn the line number into a variable?   When I use $1 it thinks I mean a column.
0
 
LVL 12

Expert Comment

by:geotiger
ID: 2815388
Yes, say, if you want to print out all the lines with the first column eq 33, you can do it as

$ echo "33 34\n34 35\n33 36\n37 45" | awk '{if ($1==v) print $0}' v=33
33 34
33 36

If these lines are in a file, you can just use cat or just put it in the end.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IBM flash storage 840 15 70
Using Grep to Find a file 8 107
NTP Server in VMware 5 392
Solaris 10.  Nmap installation fails 2 77
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question