Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Grep a single line out of file?

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
epost
Asked:
epost
  • 2
1 Solution
 
geotigerCommented:
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
 
epostAuthor Commented:
Cool, how can I turn the line number into a variable?   When I use $1 it thinks I mean a column.
0
 
geotigerCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now