Cat last paragraph only command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat last paragraph only command
# 1  
Old 08-18-2015
Cat last paragraph only command




Hi guys

I am learning CAT command with text files, I have learnt the basics such as finding a word and numbering but say for example I want it to display the last paragraph how would I do this with CAT commands
# 2  
Old 08-18-2015
Try this

Code:
$ tac text | awk -v RS= 'NR==1' | tac

tac is backwards cat. This will print the last line first and so on. Then, we need something that stops at paragraph ending (I assume you mean blank line). awk with a blank RS does this. There are many other ways to do this. We just need something that prints until the determined ending and then quits (or stops printing). Then of course we need to put the lines back in order again with tac.

Another way is to read the file twice with awk. The first time to find out where the last paragraph is, and the next time to print it.
Code:
$ awk -v RS= 'NR==FNR{last=FNR;next}FNR==last' text text

Many awks keep $0 in the END block. You can make it very simple this way:
Code:
$ awk 'END{print $0}' RS= text

Of course none of these use cat. But cat can't do much anything except print. There's not any logic.
# 3  
Old 08-19-2015
Quote:
Originally Posted by steve2015
Hi guys

I am learning CAT command with text files, I have learnt the basics such as finding a word and numbering but say for example I want it to display the last paragraph how would I do this with CAT commands
cat does absolutely none of these things.

I suspect you have been practicing your useless use of cat instead. Please realize no command needs cat's help to read a file, making cat almost always redundant and pointless except for its actual purpose of concatenating files together.
# 4  
Old 08-19-2015
Try:
Code:
awk '{p=$0} END{print p}' RS= file

which should work if empty lines between paragraphs always consist of two consecutive newlines.

More robust would be:
Code:
awk '{p=NF?p $0 RS:x} END{printf "%s",p}' file

Which should also work if there may be spaces or tabs on the otherwise empty lines separating the paragraphs..
# 5  
Old 08-19-2015
Thank you for helping me with this guys, will look into tac and use it more
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

cat command

Hi. How can i write a command on AIX like the one i did at linux that find string in a file and show me that string, and return 3 lines before and 4 lines after that string. my linux command is: /bin/cat <filename> | tail -150 | grep -B2 -A8 "<string to look for>" Example: /bin/cat ... (10 Replies)
Discussion started by: yechi_r
10 Replies

2. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

3. Shell Programming and Scripting

Cat command help

I want to concatenate 100 files to one file and append file name in each record to find out which file it came from for a in $(<shal_group) do cat $a >> bigoutput.group The above code put all files in one file but i want file name appended to each file Record should be like this... (3 Replies)
Discussion started by: pinnacle
3 Replies

4. UNIX for Advanced & Expert Users

cat command

I believe I used the cat command to append a file beside another file (instead of below it) but I did not document it any where and I can't remember exactly how I did it. Has anyone else done this? I have tried all the cat options individually with no luck. It may be a combination of options. ... (2 Replies)
Discussion started by: nickg
2 Replies

5. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

6. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

7. AIX

cat command

I would like to append some statement into 1 single file so that it can be concatenate together in 1 word. I have tried >> but it will seperate my 2 statement into 2 rows. # cat abc.txt cde.txt > result.txt where abc.txt is "abcde" and cde.txt is "12345" the result should come out as... (3 Replies)
Discussion started by: kwliew999
3 Replies

8. UNIX for Dummies Questions & Answers

CAT command

All - how do i save the file after i used CAT command line to modify? Thanks :confused: (2 Replies)
Discussion started by: March_2007
2 Replies

9. Shell Programming and Scripting

cat command

What does the below command means $cat <<% >abc.txt (3 Replies)
Discussion started by: surjyap
3 Replies

10. Shell Programming and Scripting

the CAT command

hi everybody, how do i open a txt file writen in unix on to a web page so when i want to view the txt file that was generated from a shell program, that file is open on a web page do i use the cat > filename.html command to do this, or is there another way many thanks :D (2 Replies)
Discussion started by: alexd
2 Replies
Login or Register to Ask a Question