Visit Our UNIX and Linux User Community

Adding EMPTY columns to Tab-delimited txt file

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding EMPTY columns to Tab-delimited txt file
# 1  
Old 02-27-2009
Adding EMPTY columns to Tab-delimited txt file


I have a txt file with 4 columns where I need to add 4 empty columns in the middle meaning that I need what is currently column 4 to be column 8 in a new file.

The idea is that I have to use the file as input in a program that reads the data in column 1 and 8, so the content of the other columns doesn't matter.

I am a total newbie but have tried something like:

gawk '{print $1,"\t ","\t ","\t ","\t " , $2 , $3 , $4}' filename.txt > test

And this adds space between the original columns 1 and 2 but apparently this doesn't qualify as being columns but just "space".

I keep getting the following error when running my program(and there's nothing wrong with that command because it works with a file with the correct number of columns):

Traceback (most recent call last):
File "/home/people/lunde/python/", line 349, in ?
HLAcoverindex, popcoverlist = readpop(open(opts.coveragefilename, 'r'))
File "/home/people/lunde/python/", line 105, in readpop
coverage = float(fields[7])
IndexError: list index out of range

Hope someone can help me out. Please!
# 2  
Old 02-27-2009
Can you post a small sample of the input file?

something like the results of
cat -vet filename.txt | head

# 3  
Old 02-28-2009
This is the input file where the current column 4 needs to become column 8:

cat -vet | head

A*0201^IIreland Northern^I46.8^I0.274^I^I^I^I^I$
A*0101^IIreland Northern^I36.4^I0.202^I^I^I^I^I$
A*0301^IIreland Northern^I26.3^I0.143^I^I^I^I^I$
A*1101^IIreland Northern^I15.0^I0.080^I^I^I^I^I$
A*24020101^IIreland Northern^I12.0^I0.061^I^I^I^I^I$
A*2902^IIreland Northern^I9.5^I0.049^I^I^I^I^I$
A*3201^IIreland Northern^I6.1^I0.032^I^I^I^I^I$
A*310102^IIreland Northern^I5.1^I0.026^I^I^I^I^I$
A*2501^IIreland Northern^I3.9^I0.020^I^I^I^I^I$
A*680102^IIreland Northern^I3.9^I0.020^I^I^I^I^I$
# 4  
Old 02-28-2009
Is the fieldseparator a "^" sign? Better is to post your input file and the desired output.

# 5  
Old 02-28-2009
See here's the problem with being a total newbie - don't know what defines the fieldseperator.
I just now that the ^| indicates tabs and the $ is the end-of-line-indicator or something like that. Never used -vet with cat before so only just found that out this morning. :-)

But the input file is saved as tab-delimited txt from an excel file meaning I have four columns like:

A*0201 Ireland Northern 46.8 0.274
A*0101 Ireland Northern 36.4 0.202

and I wan't

A*0201 Ireland Northern 46.8 xx xx xx xx 0.274
A*0101 Ireland Northern 36.4 xx xx xx xx 0.202

The position and content of the extra four columns(or the middle 6 columns) doesn't matter - I just need to create a Tab-delimited txt-file with A*0201 in column 1 and 0.274 in column 8.
I could start from the beginning and add the extra columns in the original excel file but I wan't to know how to do it using only Unix.

Really want to learn this but have zero experience with programming of any kind....
# 6  
Old 02-28-2009
You may try with empty fileds (use nawk or /usr/xpg4/bin/awk on Solairs):

HTML Code:
awk -F'\t' '{$8=$4;$4=_}1' OFS='\t' infile

awk -F'\t' '$NF="x\tx\tx\tx\t"$NF' OFS='\t' infile

# 7  
Old 02-28-2009
How cool was that!! It worked! Thanks!

Used this command:

gawk -F'\t' '{$8=$4;$4=_ }1' OFS='\t' myfile > Newfile

And got the desired output which my program reads as it should:
A*0201 Ireland Northern 46.8 0.274
A*0101 Ireland Northern 36.4 0.202
A*0301 Ireland Northern 26.3 0.143

But well I would like to understand it so that I would be able to use it sometime in a different but similar problem.

So if the -F'\t' part defines the fieldseperator what does the OFS='\t' part do and why is it necessary with both parts?

And what does the ;$4=_)1 part of the command do?

Previous Thread | Next Thread
Test Your Knowledge in Computers #743
Difficulty: Medium
The AMD Optimizing C/C++ Compiler (AOCC) is a free, open source, optimizing compiler from AMD.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Delete and insert columns in a tab delimited file

Hi all , I have a file having 12 columns tab delimited . I need to read this file and remove the column 3 and column 4 and insert a word in column 3 as "AVIALABLE " Is there a way to do this . I am trying like below Thanks DJ cat $FILENAME|awk -F"\t" '{ print $1 "\t... (3 Replies)
Discussion started by: Hypesslearner
3 Replies

3. Shell Programming and Scripting

Insert empty columns inside a pipe delimited file

Hi All , I have pipe delimiter file with 11 columns . I need to insert 4 empty columns after column 10 . and After 11 column I need to insert a column which is having the same value for all the rows . My file 1|2|3|4|5|6|7|8|9|10|11 New file ... (11 Replies)
Discussion started by: Hypesslearner
11 Replies

4. Shell Programming and Scripting

Remove blank columns from a tab delimited text file

Hello, I have some tab delimited files that may contain blank columns. I would like to delete the blank columns if they exist. There is no clear pattern for when a blank occurs. I was thinking of using sed to replace instances of double tab with blank, sed 's/\t\t//g' All of the examples... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

5. UNIX for Dummies Questions & Answers

How to add a header to a tab delimited .txt file?

Hi, I have a tab delimited document with 18 columns. My file looks like: comp1000201_c0_seq1 comp1000201_c0 337 183.51 0.00 0.00 0.00 0.00 ---NA--- 337 0 0 - comp1000297_c0_seq1 comp1000297_c0 612 458.50 ... (1 Reply)
Discussion started by: alisrpp
1 Replies

6. UNIX for Dummies Questions & Answers

Removing empty lines at the end of a Tab-delimited file

I'm trying to remove all of the empty lines at the end of a Tab delimited file. They have no data just tabs. I've tried may things, here are a couple: sed /^\t.\t/d File1 > File2 sed /^\t{44}/d File1 > File2 What am I missing? (9 Replies)
Discussion started by: SirHenry1
9 Replies

7. UNIX for Dummies Questions & Answers

Deleting columns from a tab delimited text file?

I have a tab limited text file with 10000+ columns. I want to delete columns 6 through 23, how do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

9. UNIX for Dummies Questions & Answers

nth Columns in a Tab delimited file

Hi Can anyone help me to identify the nth field from a Tab delimited file? Thanks Subrat (8 Replies)
Discussion started by: subrat
8 Replies

10. Shell Programming and Scripting

Reading columns in tab delimited file

I want to read only one column in "|" delimited file and write that column to a new file. For Ex: Input File 1|abc|324|tt 2|efd|11|cbcb 3||1|fg 4|ert|23|88 Output : I want to read column 3 in diff file. 324 11 1 88 Can anyone give me inputs on this ? (2 Replies)
Discussion started by: net
2 Replies

Featured Tech Videos