How to reverse the contents of a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to reverse the contents of a file?
# 8  
Old 06-05-2007
attached is a compilation of different methods.

Last edited by vgersh99; 03-04-2010 at 01:54 PM..
# 9  
Old 06-06-2007
Quote:
Originally Posted by aajan
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
By reverse, you mean character by character or line by line?
# 10  
Old 05-16-2008
Try playing with the -r flag on tail.
# 11  
Old 05-17-2008
Just out of curiosity, you can use shell constructs for this, too.

Code:
#!/bin/sh

tac () {
  local line
  if read line; then
    tac
    printf '%s\n' "$line"
  fi
}

for f; do
  tac <"$f"
done

ksh could use print instead of printf; tested with bash. (Using echo is not very robust, but would be compatible even with the earliest Bourne shells. But then, local is a fairly recent addition, isn't it?)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

4. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

5. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 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. UNIX for Dummies Questions & Answers

Reverse Contents Issue

Hi Guys, I am a newbie to Unix. I am having a requirement. I am having a file whose contents are mentioned below: S.no EmployeeName EmployeeID 1. MAHESH 01 2. SHANKAR 02 3. SATHEESH 03 4. RANJITH 04 5. SYED ... (1 Reply)
Discussion started by: mraghunandanan
1 Replies

8. 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

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

Creating file contents using contents of another file

Hi, I am not sure how to start doing this so I hope to get some advice as to how to start. I have 2 files. The source file contains data that I needed is in columns delimited by ";". For example, in this format: "CONTINENT","COUNTRY","CITY","ID" "asia","japan","tokyo","123"... (21 Replies)
Discussion started by: ReV
21 Replies
Login or Register to Ask a Question