Solved

Grep a single line out of file?

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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…
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…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now