Jump to content

Recommended Posts

Posted

I have a log file I want to be able to read only a specific line from..

 

It looks like this:

 

rb@linux:~/rrb_test> cat output.txt 

1          
-----------
        18

 1 record(s) selected.

 

How can I use cat / awk / sed whatever to take only the line that has 18 in it and save that to another file please?

Posted (edited)

Tell awk to print the 3rd line.

awk '{ if (NR==3) print $0 }' output.txt

 

 

If you want to remove spaces,

awk '{ if (NR==3) print $0 }' output.txt | sed 's/ //g'

 

 

Or you could use

tail -n3 output.txt

 

 

 

If the file could be variable length, then you'll want the grep with the 'After' switch to print the line after your match. The tail prints the last line - the second line of the two-line output.

 

grep -A1 -- "-----------"  output.txt | tail -n1

 

The two hyphens before the search string are to stop grep expecting parameters, as the search string is the 'paramaters ahoy!' character. Put the sed at the end to strip spaces.

 

Saving the output is easy. Just pipe the result to a file with

>newfile.txt

or append with

>>newfile.txt

 

full example:

 awk '{ if (NR==3) print $0 }' output.txt | sed 's/ //g' >> results.txt

Edited by jinnantonnixx
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...