Help rearrange the content of the file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help rearrange the content of the file.
# 1  
Old 07-17-2008
Help rearrange the content of the file.

Hi all,

I need to rearrange the content of the file. I need to move line 1 to 4, 2 to 3, 3 to 2, and 4 to 1.
IPlease help. I was thinking about using head and tail command.

Here is the original file.

aa
bb
cc
dd

Here is what I need it to be.

dd
cc
bb
aa

Thanks.
# 2  
Old 07-17-2008
sort the file as shown will work

If the file is just the contents as you show and you want it to be sorted in reverse order, then...

cat file|sort -kr1,1
# 3  
Old 07-17-2008
look into 'man rev'
# 4  
Old 07-17-2008
I don't have rev command on my unix box.
# 5  
Old 07-17-2008
many ways to skin that cat - pick your poison:
  1. sed -n '1!G;h;$p' datafile
  2. sed '1!G;h;$!d' datafile
  3. nl -ba datafile | sort -nr | cut -f2-
  4. perl -e 'print reverse <>' file
  5. tail -r datafile
  6. awk '{ a[NR]=$0 } END { for(i=NR; i; --i) print a[i] } ' datafile
# 6  
Old 07-17-2008
continued ... Smilie

if tac is available:

Code:
tac input

in place:

Code:
ed - input<<!
g/^/m0
w
q
!

# 7  
Old 07-17-2008
Code:
#!/bin/ksh

   cnt=0
   while read line
   do
      ((cnt=$cnt+1))
      rr[cnt]=$line
   done < datafile

   while (( $cnt > 0 ))
   do
      print ${rr[$cnt]}
      ((cnt=$cnt-1))
   done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rearrange fields of delimited text file

I want to rearrange the fields of delimited text file after sorting first line (only): input file: a_13;a_2;a_1;a_10 13;2;1;10 the result should be: a_1;a_2;a_10;a_13 1;2;10;13 any help would be appreciated andy (20 Replies)
Discussion started by: andy2000
20 Replies

2. Shell Programming and Scripting

Rearrange a file (2000 base64 strings in 1 row into 1 string by rows)

I have 1 row which contains abouts 20000 base64 string. e.g: /p4bdllBS8qcvW/69GUYej8nEv6gwt7UAYl0g==WZdjwTUQX9UEKsT/zWaZdQ==uI would like rearrange this file by base64 strings. So the output should be this ( 1 string in 1 row): 69GUYej8nEv6gwt7UAYl0g== WZdjwTUQX9UEKsT/zWaZdQ==How could I do... (4 Replies)
Discussion started by: freeroute
4 Replies

3. UNIX for Dummies Questions & Answers

Transpose matrix, and rearrange columns common with another file

This is my first post, I apologize if I have broken rules. Some assistance with the following will be very helpful. I have a couple of files, both should ultimately have common columns only, arranged in the same order. This file needs to be transposed, to bring the rows to columns ... (2 Replies)
Discussion started by: abh.kumar
2 Replies

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

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

6. Shell Programming and Scripting

Rearrange the text file

Gents, I have a large file and each line of the file contains more than 200 bytes.Please let me a way to have the new line to start when the word "FIT" appears. I was trialling with 'tr' command but i am not sure how to get it based on bytes and so it wasn't working... Current... (3 Replies)
Discussion started by: appu2176
3 Replies

7. Shell Programming and Scripting

Rearrange data from 2 files

Dear All, I have the below files A file contains 1473 1649 1670 1758 1767 1784 B file contains 1242 1246 1264 1268 1284 (3 Replies)
Discussion started by: yahyaaa
3 Replies

8. Shell Programming and Scripting

rearrange a file

hi! in awk, i have a file like this: Trace1: WRIT,Trace2: BLAN,Trace3: BLAN, -47.2120018005371,,,39815.4809027778 -46.3009986877441,,,39815.4809027778 -46.277000427246,,,39815.4809143519 -46.7389984130859,,,39815.4809259259 -46.3460006713867,,,39815.4809259259... (10 Replies)
Discussion started by: riderman
10 Replies

9. Shell Programming and Scripting

rearrange info of file in a "table"

Please I need to rearrange data acquired by USB port from a sensor network. The information is mixed and I need to convert it into a kind of table. This is my input file: Node 4D5A joined Temperature: 27,5 Humidity: 40 Dew Point: 23 No motion detected LUX: 389 Temperature: 28 Humidity: 41... (5 Replies)
Discussion started by: csecnarf
5 Replies

10. UNIX for Dummies Questions & Answers

Rearrange bytes within a txt file

I have a text file with a very long list of dates, one date per line. Each date is in the same format yyyymmdd. I need the dates to be in mmddyyyy format. This is part of a larger tcsh shell script. I am just learning unix and came up with: for each line in file for each character in... (4 Replies)
Discussion started by: yankee428
4 Replies
Login or Register to Ask a Question