Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using awk to split file with one field into a file with n fields

Status
Not open for further replies.

bongo

Technical User
May 8, 2001
2
US
I have a file that has 3000 lines of text all in one field records. How do I split the file such that certain records are each assigned to a new field.<br>Example:<br>Old file:<br>000<br>aaaaaa<br>123<br>bbbbbb<br>345<br>ccccc<br>567<br>ddddd<br>789<br>eeeee<br>101112<br>fffff<br>121314<br>ggggg<br>141516<br>hhhhh<br>New file:<br>000&nbsp;&nbsp;aaaaaa&nbsp;&nbsp;bbbbbb&nbsp;&nbsp;cccccc dddddd<br>789&nbsp;&nbsp;eeeeee&nbsp;&nbsp;ffffff&nbsp;&nbsp;gggggg hhhhhh
 
#!/usr/bin/sh<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File: concat<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Usage: concat infile outfile<br>#<br>#<br><br>awk '{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 1) {line=$0;getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 2) {line=line&quot; &quot;$0;getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 3) {getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 4) {line=line&quot; &quot;$0;getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 5) {getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 6) {line=line&quot; &quot;$0;getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 7) {getline}<br>&nbsp;&nbsp;&nbsp;&nbsp;if(++rn == 8) {line=line&quot; &quot;$0;rn=0;print line}<br><br>}' &lt; $1 &gt; $2<br><br>This will work for the examples you provided.<br><br>Hope this helps you.<br><br> <p>flogrr<br><a href=mailto:flogr@yahoo.com>flogr@yahoo.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top