Include information and change column order while maintaing white spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Include information and change column order while maintaing white spaces
# 1  
Old 06-16-2014
Include information and change column order while maintaing white spaces

I need to change the order of the columns of data that looks like this:
The original data is in 6 tab-separated columns.

Code:
FACTSHEET       factsheet       NN      1       5       DEP
WHAT    what    WP      2       3       SBJ
IS      be      VBZ     3       1       NMOD
AIDS    AIDS    NP      4       0       ROOT
?       ?       SENT    5       0       ROOT


AIDS    AIDS    NP      1       23      DEP
(       (       (       2       0       ROOT
Acquired        Acquired        NP      3       0       ROOT
Immune  Immune  NP      4       0       ROOT
Deficiency      Deficiency      NP      5       8       VMOD
Syndrome        Syndrome        NP      6       8       SBJ
)       )       )       7       8       P
is      be      VBZ     8       0       ROOT
a       a       DT      9       10      NMOD
condition       condition       NN      10      8       PRD
caused  cause   VVN     11      10      NMOD

I need to re-format the column to look like this:
Code:
1       FACTSHEET       -       factsheet       NN      -       5       DEP    --
2       WHAT    -       what    WP      -       3       SBJ     -       -
3       IS      -       be      VBZ     -       1       NMOD    -       -
4       AIDS    -       AIDS    NP      -       0       ROOT    -       -
5       ?       -       ?       SENT    -       0       ROOT    -       -

1       AIDS    -       AIDS    NP      -       23      DEP     -       -
2       (       -       (       (       -       0       ROOT    -       -
3       Acquired        -       Acquired        NP      -       0       ROOT   --
4       Immune  -       Immune  NP      -       0       ROOT    -       -
5       Deficiency      -       Deficiency      NP      -       8       VMOD   --
6       Syndrome        -       Syndrome        NP      -       8       SBJ    --

So far, I have been using the following
Code:
awk '{print $4 "\t" $1 "\t" "-" "\t" $2 "\t" $3 "\t" "-" "\t" $5 "\t" $6 "\t" "-" "\t" "-"}' input > ouput

But, this code actually prints the "-" where the white spaces are, like this:

Code:
                -                       -                       -       -
                -                       -                       -       -
1       FACTSHEET       -       factsheet       NN      -       5       DEP    --
2       WHAT    -       what    WP      -       3       SBJ     -       -
3       IS      -       be      VBZ     -       1       NMOD    -       -
4       AIDS    -       AIDS    NP      -       0       ROOT    -       -
5       ?       -       ?       SENT    -       0       ROOT    -       -
                -                       -                       -       -
                -                       -                       -       -
1       AIDS    -       AIDS    NP      -       23      DEP     -       -
2       (       -       (       (       -       0       ROOT    -       -
3       Acquired        -       Acquired        NP      -       0       ROOT   --
4       Immune  -       Immune  NP      -       0       ROOT    -       -
5       Deficiency      -       Deficiency      NP      -       8       VMOD   --
6       Syndrome        -       Syndrome        NP      -       8       SBJ    --

Is there an `awk` that would permit me to do this and maintain the whitespaces?
# 2  
Old 06-16-2014
You could modify your awk like this:
Code:
awk '!NF{print; next}{print $4 "\t" $1 "\t" "-" "\t" $2 "\t" $3 "\t" "-" "\t" $5 "\t" $6 "\t" "-" "\t" "-"}' FS='\t' file

or

Code:
awk '!NF{print; next}{print $4, $1, "-", $2, $3, "-", $5, $6, "-", "-"}' FS='\t' OFS='\t' file


Last edited by Scrutinizer; 06-16-2014 at 08:23 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Help with using tr - Removing white spaces

Hi, I have a file that contains whitespaces with spaces and spaces and tabs on each line and am wanting to remove the whitespaces. My version of sed is one that does not recognize \t etc. The sed and awk one-liners below that I found via Google both does not work. So my next best... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

3. Shell Programming and Scripting

Include white spaces while using CUT command

Hi I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing. file 1 <name>Kumar Rajasekaran</name> cut -c7-26 "file1" the out put i received is ... (48 Replies)
Discussion started by: Sekar1
48 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Delete Multiple White Spaces

Hi, I have a file that has multiple spaces between characters. I want to delete or convert the multiple spaces into a single space. I think this can be done in "sed" but I only know the syntax to delete trailing or leading spaces. Can this be done with "sed" or awk? I have a file that looks... (6 Replies)
Discussion started by: eja
6 Replies

6. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies

7. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

8. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

9. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies
Login or Register to Ask a Question