Padding lines in a file with newlines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Padding lines in a file with newlines
# 1  
Old 02-04-2010
Padding lines in a file with newlines

Hi Guys,

I have a file as shown below:

Code:
55
6
77
8
88
98
7
85
45
544
1
33
32
13
13
23
44
11
67
87
243
56
344
23
133
1343
12
10
1155
321
15
899
654

The data is supposed to be arranged in a group of 10 values. Some groups do not have all 10 values.

So for example the following numbers belong to one group:

Code:
55
6
77
8
88
98
7
85
45
544

The following set of numbers belongs to another group but does not have all 10 values:

Code:
1
33
32
13

I would like to stuff this group with blank lines.

So my final output file should look like this:

Code:
55
6
77
8
88
98
7
85
45
544
1
33
32
13
 
 
 
 
 

13
23
44
11
67
87
 
 
 

243
56
344
23
133
1343
12
10
 
 
1155
321
15
899
654

So group 1 had all 10 values, group 2 (numbers 1,33,32,13) had only 4 values hence, padding of 6 more newlines to make it 10; group 3 (13,23,44,11,67,87) has only 6 values so padd it with 4 newlines to make it 10, etc.

Next I want to collect all these values in an array including the empty lines as a null array element.

How can I do this?

Thanks for your help.
# 2  
Old 02-04-2010
How do you group numbers?
# 3  
Old 02-04-2010
These numbers are extracted from another file which has groups of 10. All I want is to pad the file which has missing entries, with newlines. Will it be easier if I modify the original file as follows:

Code:
55
6
77
8
88
98
7
85
45
544

1
33
32
13

13
23
44
11
67
87

243
56
344
23
133
1343
12
10

1155
321
15
899
654

So now I have inserted a newline between individual groups.Group1 has all 10 entries, group 2 has only 4 entries (that means I will have to add 6 newlines), group 3 has 6 entries (so I will have to add 4 lines) and so on.

Hope this helps.
# 4  
Old 02-05-2010
If your groups are paded with a newline as in your 2nd example, this works :
Code:
#!/bin/bash
while read L
do
	[ -n "$L" ] && { ((i++)); echo $L; continue; }
	until [ $i -eq 10 ]
	do	((i++)); echo
	done
	i=0
done < infile

# 5  
Old 02-05-2010
Thanks, that worked.

Can you please explain the syntax? Appreciate your help.
# 6  
Old 02-05-2010
Code:
while read L # reads on lie from input and ssign it to L
do
    [ -n "$L" ] && { ((i++)); echo $L; continue; }
        # if L is not empty then increment i, output L, next iteration of the loop
    until [ $i -eq 10 ] # doesn't need explanation
    do    ((i++)); echo 
    done
    i=0 # reset i for the next 10 count
done < infile

If you want to see what happens during the running of the script, you can
Code:
bash -x scriptname

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX file with Newlines

Hi Friends, I have a data file with new lines. How to remove the newlines and should be showed in one line. I tried using the command tr -d '\n' filename sed 's/\n//g' file name Ex: 1 abc hyd is actual record but in our scenario showing it as 1 abc hydthis record should be like... (5 Replies)
Discussion started by: victory
5 Replies

2. Shell Programming and Scripting

File formatting with newlines

Hi All - I am in need of some help in formating the below file Requirement - 1) replace newlines with space 2) replace '#~# ' with newline ----------------------- sample inputfile a I|abc|abc|aaa#~# I|sddddd|tya|dfg sfd ssss#~# I|tya1|tya2|dfg|sfd|aaa#~#... (5 Replies)
Discussion started by: J1nx007
5 Replies

3. UNIX for Beginners Questions & Answers

Remove newlines and carriage return from a csv file using UNIX

I need to remove new lines and carriage returns from csv file. Is there anything other than sed and gwak by which we could achieve this ? Any suggestions ? (3 Replies)
Discussion started by: A_Gaddale
3 Replies

4. Shell Programming and Scripting

'for LINE in $(cat file)' breaking at spaces, not just newlines

Hello. I'm making a (hopefully) simple shell script xml parser that outputs a file I can grep for information. I am writing it because I have yet to find a command line utility that can do this. If you know of one, please just stop now and tell me about it. Even better would be one I can input... (10 Replies)
Discussion started by: natedawg1013
10 Replies

5. UNIX for Dummies Questions & Answers

finding string in very long file without newlines

What's the best way to find a string in a very long file without newlines in Unix? The standard utility I'm aware of for finding a string in a single file is grep, but for a long file without newlines, I think the output is just going to be the input. I suppose I could use sed to replace the... (5 Replies)
Discussion started by: aaronpoley
5 Replies

6. Shell Programming and Scripting

fixed length text file padding issues in AIX

Hi, I have a fixed length text file that needs to be cut into individual files in aix and facing padding issues. If I have multiple blank spaces in the file it is just making it one while cutting the files.. Eg:- $ - blank space filename:file.txt ... (2 Replies)
Discussion started by: techmoris
2 Replies

7. Shell Programming and Scripting

Removing inserted newlines from a fileld of fixed width file.

Hi champs! I have a fixed width file in which the records appear like this 11111 <fixed spaces such as 6> description for 11111 <fixed spaces such as 6> some more field to the record of 11111 22222 <fixed spaces such as 6> description for 22222 <fixed spaces such as 6> some more field to the... (8 Replies)
Discussion started by: enigma_1
8 Replies

8. Shell Programming and Scripting

Imagemagick File Padding Issue

Hello, I'm having a problem figuring out the syntax for padding 10-99. Everything else in the program works fine so I want to focus in on just this part of the code. Below is a snippet of the code that I am having problems with. I appreciate all the help I can get. Thank you. The script... (7 Replies)
Discussion started by: jsells20
7 Replies

9. UNIX for Advanced & Expert Users

Padding Carriage return to the end of XML file

Hi All, I am getting a xml file where the first field contains a carriage return and the all other fields doesnot contains any carriage return. So all the other records comes in the second line. <?xml version="1.0" encoding="UTF-8"?> <ns0:iSeriesCspIntegration... (3 Replies)
Discussion started by: dasj22
3 Replies

10. HP-UX

Padding zeros after removing commas in file

Hi Gurus, There is a ASCII file in which a comma is used as a seperator for the amount field when the amount exceed seven digits: e.g. 0001300,000. Now, this comma needs to be removed from this field, after padding leading zeros (to maintain the ASCII positions) e.g. 00001300000.... (1 Reply)
Discussion started by: pranag21
1 Replies
Login or Register to Ask a Question