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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to split a line 1

Status
Not open for further replies.

awk

Programmer
Feb 3, 2002
3
0
0
US
Hi,
I have a piece of program code and the longest statement has over 2000 characters, I want to split each line into lines that are 80-100 character longs without splitting any words. That is, I need to introduce a line-break somewhere after the first whitespace which is after the 80th character in the line. How do I do that ??

Thanks in advance,
judia
 
Hi judia!

Awk is suitable for this problem.

Please, try this awk solution:


# split.awk - splits lines (setting length of line with lnLen)
# split.awk - croatian: prijelom redaka (podesavanje duzine retka - lnLen)
# Kruno Peter, kruno_peter@yahoo.com
# awk, Public Domain, August 2001
# Jesus loves you.

BEGIN { lnLen = 79 }

length > lnLen {
for (i = 1; i <= NF; i ++) {
if (i > 1) {
row = tmpRow
tmpRow = tmpRow &quot; &quot; $i
}
else {
row = $i
tmpRow = $i
}
if (length(tmpRow) > lnLen) {
print row
row = &quot;&quot;
tmpRow = $i
}
}
if (tmpRow != &quot;&quot;)
print tmpRow
}

length < lnLen { print }


Variable lnLen can be command-line argument.

Bye!

KP.
 
Krunek,

Can you explain what your script does?

Three
 
how 'bout a recursive split?!

I've hardwired the &quot;length&quot; just for testing

vlad

---------------------- splitLine.awk -----------------------

function splitLine(line)
{
str=substr(line, len);
idx=index(str, &quot; &quot;);
if (idx == 0)
printf(&quot;[%s]\n&quot;, line);
else {
printf(&quot;[%s]\n&quot;, substr(line, 1, len+idx-1));
splitLine(substr(line, len+idx));
}
}

BEGIN {
len=6;
}

{
splitLine($0);
}

END {

}

---------------------- splitLine.awk -----------------------


----------------------- splitLine.txt -------------
123 45 6 789
1 2 345 67 89
----------------------- splitLine.txt -------------
 

Yes, Three,

I'll try to explain this program in plain English (to the best of my knowledge of English; but it's tinny). ;-)

This awk script splits lines of text file whose length exceeds 79 characters; this value sets a variable lnLen. You can change this value as you wish or change this awk script to take command-line argument.

Program just prints lines whose length is less than value of lnLen.

Second statement, length > lnLen { ... }, processes lines whose length exceeds 79 characters. I must post this statement one more time, because indentation is bad in previos message (I removed tabs):


length > lnLen {
for (i = 1; i <= NF; i ++) {
if (i > 1) {
row = tmpRow
tmpRow = tmpRow &quot; &quot; $i
}
else {
row = $i
tmpRow = $i
}
if (length(tmpRow) > lnLen) {
print row
row = &quot;&quot;
tmpRow = $i
}
}
if (tmpRow != &quot;&quot;)
print tmpRow
}


Enought? If you have more questions, just post it.

Bye!

KP.
 
Krunek,

Thanks for the explanation. This works great for me, but what if I have HTML tags such as <longtag name that cant be split in two lines> or other tags that I dont want to seperate into two lines. Basically keep all charaters the same length as what you wrote in your awk but leave the long tags (if they go over 79) on one line.

Is that possible?
 
Krunek,

Please disregard my last question...I might not need to worry about it.

Can you explain more about your code such as:


length > lnLen {
for (i = 1; i <= NF; i ++) {
if (i > 1) {
row = tmpRow #WHAT IS HAPPENING HERE?
tmpRow = tmpRow &quot; &quot; $i #WHAT IS THIS DOING?

else {
row = $i #IS THIS THE ELSE FOR i < 1 and would it then be a blank row?
tmpRow = $i #WHAT ROW IS THIS?

if (length(tmpRow) > lnLen) {
print row #PRINTING WHAT ROW?
row = &quot;&quot; #PRINTING BLANK ROW?
tmpRow = $i #WHAT DOES THIS PRINT?


if (tmpRow != &quot;&quot;) #IF tmpRow is not a blank row?
print tmpRow #THEN PRINT ROW THAT IS UNDER 79?

Thanks!
 


Hi three,

I'll think about your suggestion about long tags.

Yes, I can explain some details from my code (to the best of my knowledge of English).

My program works in for loop with words (fields) of line.

Variable 'row' contains string for output; program uses variable 'tempRow' in condition.

awk program assembles here output line:

if (i > 1) {
            row = tmpRow
            tmpRow = tmpRow &quot; &quot; $i
        }
        else {
            row = $i
            tmpRow = $i
        }

I can't use in program expression 'tmpRow = tmpRow &quot; &quot; $i' for 1st word of line, because it produces output
xxxxx
(blank and word). So, my program uses expression 'tmpRow = $i' for first word of input line.

This construct controls printing of the output line; program prints output line, if length of 'tempRow' exceeds 'lnLen' characters:

if (length(tmpRow) > lnLen) {
            print row
            row = &quot;&quot;
            tmpRow = $i
        }


I'll be back!

KP.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top