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

New lines

Status
Not open for further replies.

sabetik

IS-IT--Management
Nov 16, 2003
80
GU
How can I break a line in two lines. I have a file like this:

xxxxx xxxxxx xxxx.xx xxxxx xxxxxx xxxx.xx
xxxxx xxxxxx xxxx.xx xxxxx xxxxxx xxxx.xx
I need to make then in one line

Ex:
xxxxx xxxxxx xxxx.xx
xxxxx xxxxxx xxxx.xx
xxxxx xxxxxx xxxx.xx
xxxxx xxxxxx xxxx.xx

Thanks in advance
Kamranh
 
Hi:

I think you're asking for a newlne after every third field:


awk ' { for(i=1; i<=NF; i++)
if( (i%3) == 0)
printf("%s \n", $i)
else
printf("%s ", $i)
} ' data.file


 
It looks as though the 3rd field is separated from the 4th field by 8 spaces or a tab character. If that is true, then this should work:
Code:
BEGIN { FS="(        |\t)"; OFS=RS }
$1=$1
Explanation: we set the input field-separator variable to match 8 spaces or a tab; so after a line is read, $1 will be [tt]xxxxx xxxxxx xxxx.xx[/tt]. We set the output field-separator to RS, which is "\n".

The main loop is simply [tt]$1=$1[/tt]. When you make an assignment to one of the fields, Awk rebuilds $0 with [tt]OFS[/tt] between the fields.

Let me know whether this works for you.

 
futurelet and olded,
Actualy the space between 3th field and 4th field is 25 space.


BEGIN { FS="( |\t)"; OFS=RS }
$1=$1

printf("%s ", $1) > "newdata.txt"

}
Is this right.
Thanks


 
Code:
## Break lines between 3rd & 4th fields.
## Assumes those 2 fields are separated by 10 or
## more spaces or by a tab and optional spaces.
BEGIN { OFS=RS
  FS="          +| *\t *" }
$1=$1

Save as "newlines.awk" and run with
[tt]awk -f newlines.awk infile >outfile[/tt]

If there is a gap of 10 spaces between fields other than $3 and $4, this won't work properly. In that case, use Olded's program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top