reverse the file content blockwise


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reverse the file content blockwise
# 1  
Old 11-28-2011
reverse the file content blockwise

Hi,

I am having a file with some data present in blocks
e.g.

Code:
----------------
some data
some more data
couple of more lines.
----------------
another set of data
more lines
----------------
even more data
more lines
----------------

what I need is reverse the data.
like..
Code:
----------------
even more data
more lines
----------------
another set of data
more lines
----------------
some data
some more data
couple of more lines.
----------------

the number of blocks are NOT fixed.
the two blocks are separated by "----------"

Thanks.
# 2  
Old 11-28-2011
A little strange, but almost there...

Code:
$ cat sample10.txt
----------------
some data
some more data
couple of more lines.
----------------
another set of data
more lines
----------------
even more data
more lines
----------------

$ cat sample10.txt | sed 's/^\-/~\-/g' | tr "\n" "*" | tr "~" "\n" | tac | tr "*" "\n"
--------------------------------
even more data
more lines

----------------
another set of data
more lines

----------------
some data
some more data
couple of more lines.

# 3  
Old 11-28-2011
Open logic Shell script. The main issue with this file is the first line (which does not conform to the structure of the file). So we have to ignore it.

Code:
(
read junk       # Waste first record
while read line_01
do
        read line_02
        read line_03
        read line_04
        read line_05
        read line_06
        read line_07
        read line_08
        read line_09
        read line_10
        #
        echo "${line_08}"
        echo "${line_09}"
        echo "${line_10}"
        #
        echo "${line_05}"
        echo "${line_06}"
        echo "${line_07}"
        #
        echo "${line_01}"
        echo "${line_02}"
        echo "${line_03}"
        echo "${line_04}"
        #
done
) < input_file.txt

# 4  
Old 11-28-2011
Another try

Code:
$ cat sample10.txt
----------------
some data
some more data
couple of more lines.
----------------
another set of data
more lines
----------------
even more data
more lines
----------------

$ cat sample10.txt | sed 's/^\----------------/~/g'  | tr "\n" "*" | sed 's/~/~\n~/g' | tac | tr -d "\n" | sed 's/~~/~/g' | tr "*" "\n" | sed 's/~/----------------/g'
----------------
even more data
more lines
----------------
another set of data
more lines
----------------
some data
some more data
couple of more lines.
----------------

# 5  
Old 11-28-2011
Code:
gawk 'BEGIN{RS=ORS="----------------\n"}{a[NR]=$0}END{for(i=NR+1;i>1;--i)print a[i]}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reverse content

I have file and need to reverse the contents: cat filename 2345 AXY 34567 Output expects 34567 AXY 2345 (1 Reply)
Discussion started by: Maayi
1 Replies

2. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Reverse Display of a file

Hi all, Just saw a "sed" format to reverse display the file contents, but am not geting its logic completely. I would appreciate if somebody can explain sed '1!G;h;$!d' < filename All I know in this is that : G will add a new line after every line except first one... (5 Replies)
Discussion started by: dextergenious
5 Replies

4. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. Shell Programming and Scripting

searching reverse in a file

HI all, i have a file called monitor.cfg and have some sections,under these sections many parameter's are there. like..... MONITOR.CFG p1 p2 p3 #comments #comments p4 p5 p6 eof. i want to add some new parameters in section exactly at end of comments using sed. Thanks surya (4 Replies)
Discussion started by: suryanarayan
4 Replies

6. Shell Programming and Scripting

reverse sort file

Hi all I am trying to numerically reverse sort a file but I seem to be having trouble. Example of file contents: text1,1 text2,-1 text3,0 I can sort using sort -k 2n -t, filename without any problems. However I want my results in descending order but using -r in my command... (2 Replies)
Discussion started by: pxy2d1
2 Replies

7. Shell Programming and Scripting

how to reverse file

i am using AIX -ksh how can i reverse any file ,i have already try tac cmd it is not in AIX: please help me out. (3 Replies)
Discussion started by: RahulJoshi
3 Replies

8. UNIX for Advanced & Expert Users

How to reverse the contents of a file?

Hi Guys, Am new to this forum .... And also to shell scripting I need a k-shell script to reverse the contents of a file... Please come up with the solutions... With regards, Anand (10 Replies)
Discussion started by: aajan
10 Replies

9. UNIX for Dummies Questions & Answers

Reverse Arrange File

I've got hundreds of lines in a file that looks like this: Line1 CCR CCH Line2 ICVM FBO GSC Line3 MKF The result should be like the one below so that I can insert them on our database. Line1 CCR Line1 CCH Line2 ICVM Line2 FBO Line2 GSC Line3 MKF Thanks in advance! (4 Replies)
Discussion started by: The One
4 Replies

10. Shell Programming and Scripting

Need to read a file in reverse

I have to extract data from a text file which is huge in size >>10GB. ie between two strings. If I do an ordinary sed it takes forever to come out. I was wondering if there was anyway to do the entire process in reverse and on finding the relevant string is there any way to break out of the... (5 Replies)
Discussion started by: scorreg
5 Replies
Login or Register to Ask a Question