Add spaces when x-coordinate changes with Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add spaces when x-coordinate changes with Awk
# 1  
Old 11-22-2011
Add spaces when x-coordinate changes with Awk

I have a tab-delimited file with three columns. They are arranged in blocks like this

Code:
1   1   8
1   2   3
1   3   7
1   4   4
1   5   7
2   1   9
2   2   4
...

I would like to add spaces so that the file looks like this:

Code:
1   1   8
1   2   3
1   3   7
1   4   4
1   5   7

2   1   9
2   2   4
...


I also want it to skip over spaced blocks so that it does not add a second space to pre-spaced files.

I am not sure I am going about this the right way. Here is the code I have tried:


Code:
awk 'BEGIN {IFS="\t"}
{    if ($1==abscissa) {print >> "tempWF2D.OUT" $1, "\t", $2, "\t", $3}
    else {print >> "tempWF2D.OUT" "\n"}
    abscissa = $1}' WF2D.OUT


But I am getting syntax errors or it does nothing. Can anybody fix this?
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-22-2011 at 05:59 PM.. Reason: code tags, please!
# 2  
Old 11-22-2011
Code:
nawk 'FNR==1{a=$1;print;next}$1!=a{a=$1;$1=ORS $1}1' OFS='\t' myFile

# 3  
Old 11-22-2011
Quote:
Originally Posted by vgersh99
Code:
nawk 'FNR==1{a=$1;print;next}$1!=a{a=$1;$1=ORS $1}1' OFS='\t' myFile

Ok, this seems to add the spaces, but when I try to output to file with

Code:
awk 'FNR==1{a=$1;print>>"tempWF2D.OUT";next}$1!=a{a=$1;$1=ORS $1}1' OFS='\t' WF2D.OUT

it writes blank lines.
# 4  
Old 11-22-2011
Code:
nawk 'FNR==1{a=$1;print;next}$1!=a{a=$1;$1=ORS $1}1' OFS='\t' WF2D.OUT > tempWF2D.OUT  

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with split a list of records into each line with 200 coordinate at a time

Input File: E 3359799 3360148 350 X D 3471287 3471607 321 X E 3359799 3360740 942 X E 4359790 4360039 250 X . . . Desired Output File: E 3359799 3359998 200 X E 3359999 3360148 150 X D 3471287 3471486 200 X D 3471487 3471607 121 X E 3359799 3359998 200 X E 3359999 3360198 200 X... (1 Reply)
Discussion started by: perl_beginner
1 Replies

2. UNIX for Dummies Questions & Answers

Add spaces to variable

Hi, I'm passing a variable to a scrpit which can be 1 to 3 characters long. How can I force it to be three character long and add spaces to it? The passed variable is stored in $1 and I would like to be stored in NewName I tried without success NewName=$(printf "%*s 3 $1) So if... (2 Replies)
Discussion started by: f_o_555
2 Replies

3. Shell Programming and Scripting

awk for removing spaces

HI, I have a file with lot of blank lines, how can I remove those using awk?? Thanks, Shruthi (4 Replies)
Discussion started by: shruthidwh
4 Replies

4. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

5. UNIX for Dummies Questions & Answers

VIM add white spaces

Hello, How do I adda two whitespaces at the begining of each lines between line 12 and line 90; something like :12,90 ??? Thanks! (3 Replies)
Discussion started by: JCR
3 Replies

6. Shell Programming and Scripting

Help with awk adding spaces.

I have a file that contains... elm,mail elm,lisp,composer,cd,ls,cd,ls,cd,ls,zcat,|,tar,-xvf,ls,cd,ls,cd,ls,vi,ls,cd,ls,vi,elm,-f,ls,rm,ls,cd,ls,vi,vi,ls,vi,ls,cd,ls,elm,cd,ls,cd,ls,vi,vi,vi,ls,vi,ls,i,vi,ls,cp,cd,fg,ls,rm,cd,ls,-l,exit elm,mail,biff,elm,biff,elm,elm elm,ls ... (2 Replies)
Discussion started by: Bandit390
2 Replies

7. Shell Programming and Scripting

Need your HELP:: Shell script to detect paragraph in coordinate-based code.

Hi Friends!! I have obtained following output from a tool called pdftoxml: <xml> <text top="423" left="521" width="333" height="20" font="3">Although the the number of fuzzy rules of a system is </text> <text top="441" left="500" width="355" height="20" font="3">directly dependant on these... (2 Replies)
Discussion started by: parshant_bvcoe
2 Replies

8. Shell Programming and Scripting

AWK with allow me to add spaces

I have a file like this: running:running since Mon Mar 3 09:46:19 2008: running:running since Thu Mar 6 22:36:51 2008: running:running since Tue Dec 4 00:34:15 2007: running:running since Tue Dec 4 00:34:16 2007: running:running since Mon Jan 21 11:15:04 2008: running:running since... (3 Replies)
Discussion started by: rbulus
3 Replies

9. Shell Programming and Scripting

Add spaces between Characters

I need to add spaces in between characters in a string variable. Is there a shortcut? I know you can remove the spaces with sed, but does sed have a way to add them? Example: I have: DATA01 I want it to be: D A T A 0 1 What I have done so far is to create a function... (4 Replies)
Discussion started by: heyindy06
4 Replies
Login or Register to Ask a Question