Solved

Grep a single line out of file?

Posted on 2000-05-16
3
205 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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 …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

825 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