reversing order of lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reversing order of lines in a file
# 22  
Old 06-12-2009
Quote:
Originally Posted by Franklin52
Why should you use 2 temporary files and 3 external commands?
Try the awk solution.

Regards
I'm not understand this code:
Code:
 awk 'NR==FNR{a[++i]=$2 FS $3;next}{print $1,a[i--]}' file file

How I add empty columns to the above code?
# 23  
Old 06-12-2009
Quote:
Originally Posted by Max Well
I'm not understand this code:
Code:
 awk 'NR==FNR{a[++i]=$2 FS $3;next}{print $1,a[i--]}' file file

How I add empty columns to the above code?
Where do you want to add those columns (spaces)?
# 24  
Old 06-14-2009
Quote:
Originally Posted by Franklin52
Where do you want to add those columns (spaces)?
I'm not sure that is them necessary but the orginal data file (laser pulse) includes them.

A real datafile seem like this:
Code:
     0.00000000E+000    -6.16519665E-002     0.00000000E+000
     2.00000000E-002    -6.16519665E-002     0.00000000E+000
     4.00000000E-002    -6.20330853E-002     0.00000000E+000
     etc.


Last edited by Max Well; 06-14-2009 at 06:29 AM..
# 25  
Old 06-14-2009
If you want to keep the format like the original data file:

Code:
awk 'NR==FNR{a[++i]=substr($0,21,20) substr($0,41);next}
{print substr($0,1,20) a[i--]}
' OFS= file file

Regards
# 26  
Old 06-15-2009
Code:
sed -n '1 {
h
}
1 !{
x
H
}
${
x
p
}' file

# 27  
Old 07-30-2009
MySQL

Thanks!

Now I want still to reverse the line order in the third and the fourth column but also I want to regenerate lines of the first column (which includes time values of time).

From the original file (which is still same):
Code:
     0.00000000E+000    -1.17555359E-001     0.00000000E+000
     2.00000000E-002    -1.17555359E-001     0.00000000E+000
     4.00000000E-002    -1.17639683E-001     0.00000000E+000
     6.00000000E-002    -1.17655106E-001     0.00000000E+000
     8.00000000E-002    -1.17603991E-001     0.00000000E+000
     1.00000000E-001    -1.17488978E-001     0.00000000E+000

to like this:
Code:
     1.20000000E-001    -1.17603991E-001     0.00000000E+000
     1.40000000E-001    -1.17655106E-001     0.00000000E+000
     1.60000000E-001    -1.17639683E-001     0.00000000E+000
     1.80000000E-001    -1.17555359E-001     0.00000000E+000
     2.00000000E-001    -1.17555359E-001     0.00000000E+000

Finally I 'm going to merge these files.
Code:
     0.00000000E+000    -1.17555359E-001     0.00000000E+000
     2.00000000E-002    -1.17555359E-001     0.00000000E+000
     4.00000000E-002    -1.17639683E-001     0.00000000E+000
     6.00000000E-002    -1.17655106E-001     0.00000000E+000
     8.00000000E-002    -1.17603991E-001     0.00000000E+000
     1.00000000E-001    -1.17488978E-001     0.00000000E+000
     1.20000000E-001    -1.17603991E-001     0.00000000E+000
     1.40000000E-001    -1.17655106E-001     0.00000000E+000
     1.60000000E-001    -1.17639683E-001     0.00000000E+000
     1.80000000E-001    -1.17555359E-001     0.00000000E+000
     2.00000000E-001    -1.17555359E-001     0.00000000E+000

Maybe realistic method to do this process at the same time:

-Regenerate lines of the first column by iteration method
Do i=0.00000000E-001...2.00000000E-001
i=i+2.00000000E-002
end

-The second and third column:
At first print original lines.
At second print backwards from second last line to first line

How to do this in practise?

Last edited by Max Well; 07-30-2009 at 09:19 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with order lines from a file based on a pattern

Hi I need to order these lines from a txt file my file looks like this IMSI ........................ 1234567890 APN ......................... INTERNET.COM APN ......................... MMS.COM APN ......................... WAP.COM APN ......................... BA.COM IMSI... (4 Replies)
Discussion started by: alone77
4 Replies

2. Shell Programming and Scripting

Consolidate several lines of a CSV file with firewall rules, in order to parse them easier?

Consolidate several lines of a CSV file with firewall rules Hi guys. I have a CSV file, which I created using an HTML export from a Check Point firewall policy. Each rule is represented as several lines, in some cases. That occurs when a rule has several address sources, destinations or... (4 Replies)
Discussion started by: starriol
4 Replies

3. Shell Programming and Scripting

File w/ many line pairs--how do I order based on 1st lines only?

I have a file in which the data is stored in pairs of lines. The first line (beginining with ">") is a header, the second line is a sequence. I would like to sort the file by species name. Desired output for the example file: I can use sort -t'_' -k2 to alphabetize headers in the... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

4. Shell Programming and Scripting

Reversing text order of each line of a file in UNIX

My input is: hello how are you my chemistry book is lost what is up etc... And I want the output to be: you are how hello lost is book chemistry my up is what .... I found an earlier response to a similar question but it was not accurate as it required a certain string length for each line (2 Replies)
Discussion started by: Heidi Heweidy
2 Replies

5. Shell Programming and Scripting

Print selected lines from file in order

I need to extract selected lines from a log file, I can use grep to pull one line matching 'x' or matching 'y', how can I run through the log printing both matching lines in order top to bottom. i.e line 1 xyz - not needed line 2 User01 - needed line 3 123 - not needed line 4 Info - needed... (2 Replies)
Discussion started by: rosslm
2 Replies

6. UNIX for Dummies Questions & Answers

Reversing line and word order using awk

Hello, I am new to awk and I was wandering if I could reverse line and word order from a text file using awk. I figured out how to do them both separately, but can't quite figure out how to mix them. Example: Input file: dog cat mouse 1 2 3 I am new to awk Output of the awk program:... (3 Replies)
Discussion started by: blink_w
3 Replies

7. Shell Programming and Scripting

Order file by lines

My script(3 arguments $1 = folder,$2 extension,$3 string) should do the following things: -Enter in the folder of $1(if exists). -Put ls *.$2 > temp.txt ( I use a temp file to store the result of ls command and if $2 = txt in this file I'll have all the .txt files of the folder) -Now I want to... (2 Replies)
Discussion started by: Max89
2 Replies

8. Shell Programming and Scripting

reversing multiple lines

Hi I want to reverse multiple lines from my file eg of File1 3 4 5 6 7 8 9 a b c d e f g h I am using this code to reverse lines but it can only work with one row awk -F'\t' '{while (NF){printf("%s%s", $(NF--),!NF?"":FS)}}' File1 > File2 I want the file to look like this 9 8 7 6 5 4... (2 Replies)
Discussion started by: phil_heath
2 Replies

9. Shell Programming and Scripting

Reversing file order using SED

Im trying to develop a shell script that will change the content order of the file. For example I have a file that says a b c d I want to change this to be d c b a Im trying to use sed to this by reading the file and then inserting each line at the top #!/usr/bin/ksh ... (3 Replies)
Discussion started by: MBGPS
3 Replies

10. Shell Programming and Scripting

output string in reversing order

If I have string { I_love_shell_scripts} anyone knows how to have output {stpircs_llehs_evol_I} by using shell and perl ?I know in perl, there is reverse() funcation, but can it be done by not using reverse()? (3 Replies)
Discussion started by: ccp
3 Replies
Login or Register to Ask a Question