RabbieBurns Posted May 25, 2015 Posted May 25, 2015 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?
jinnantonnixx Posted May 25, 2015 Posted May 25, 2015 (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 May 25, 2015 by jinnantonnixx 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now