Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Grep a single line out of file?

Posted on 2000-05-16
3
Medium Priority
?
227 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.
Suggested Courses

705 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