Need help placing row below next to row above


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help placing row below next to row above
# 1  
Old 12-02-2012
Need help placing row below next to row above

Hi,

I would like to change this :

Code:
 kpt#  38, nband= 40, wtk=  1.00000, kpt=  0.8000  0.8000  0.7000 (reduced coord)
 -19.20800 -18.99818  -7.87808  -7.79442  -7.52754  -7.47870  -7.34989  -7.27152
  -6.83504  -6.74311  -6.66652  -6.57237  -5.55966  -5.01640  -4.21031  -4.13873
  -3.33031  -3.01657   2.45129   4.18701   6.15168   6.55283   8.46406   8.61202
   9.90925  12.12516  13.41784  14.80962  17.48936  18.38129  20.03909  20.75073
  21.99101  22.87881  23.89400  24.19821  25.81356  26.49434  26.72360  27.44037

into this

Code:
kpt#  38, nband= 40, wtk=  1.00000, kpt=  0.8000  0.8000  0.7000 (reduced coord)
 -19.20800 -18.99818  -7.87808  -7.79442  -7.52754  -7.47870  -7.34989  -7.2715 -6.83504  -6.74311  -6.66652  -6.57237  -5.55966  -5.01640  -4.21031  -4.13873 -3.33031  -3.01657   2.45129   4.18701   6.15168   6.55283   8.46406   8.61202 9.90925  12.12516  13.41784  14.80962  17.48936  18.38129  20.03909  20.75073 21.99101  22.87881  23.89400  24.19821  25.81356  26.49434  26.72360  27.44037

for all kpt#[1-N]. This means 5 strings of values will become a string. I have attached the output file which contains the raw information. I apologize in advance as I've never gotten around to properly explore sed and awk. Any help is deeply appreciated. Thanks.
# 2  
Old 12-02-2012
One way of doing it:

Code:
awk '/kpt/{print $0;next}{printf $0}!(NR%6){print ""}' file

This User Gave Thanks to ripat For This Post:
# 3  
Old 12-02-2012
Many thanks!
# 4  
Old 12-02-2012
With the given input file, ripat's suggestion works fine. But using user supplied data as a format string to printf is risky. If an input line ever happens to contain a % character followed by an alphabetic character (with an optional string of digits before the alphabetic character), you could get very different results.
An alternative way to do this (avoiding this issue) is:
Code:
awk 'NR%6<2{print}NR%6>1{printf("%s",$0)}' output.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Analyzing last 2 fields of 1 row and 3rd field of next row

I have the following script that will average the last two fields of each row, but im not sure how to include the 3rd field of the following row. An example of the analysis that I need to perform from the input - (66.61+58.01+54.16)/3 awk '{sum=cnt=0; for (i=13;i<=NF;i++) { sum+=$i; cnt++... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

2. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

3. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

4. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

5. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

6. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

7. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

8. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

9. UNIX for Dummies Questions & Answers

How to calculate the percentage/fraction of each value in a row against the maximum row value?

Hi, For each row in a file, i would like to identify the maximum value and calculate the percentage/fraction of the max for other values in the row. Then, I would like to print a copy of the file where values above a threshold are replaced with "1" and other values are left as "0". In the... (2 Replies)
Discussion started by: auburn
2 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question