Delete and insert columns in a tab delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete and insert columns in a tab delimited file
# 1  
Old 01-05-2015
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

Code:
 
cat $FILENAME|awk -F"\t" '{ print $1 "\t $2  "AVAILABLE""\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $9 "\t" $10 "\t" $11 "\t" $12 "\t"}'>>$FILENAME.tmp


Last edited by Hypesslearner; 01-05-2015 at 04:36 AM.. Reason: Given code
# 2  
Old 01-05-2015
Hello Hypesslearner,

Could you please try following and let me know if this helps.
Code:
awk '{$3="AVIALABLE";$4="\b"} 1' OFS="\t" Input_file

Thanks,
R. Singh
# 3  
Old 01-05-2015
Yes. There are several ways to delete two tab delimited fields from a file and modify the text in another field to contain the characters "AVIALABLE " (sic). Do you want to add the new text to the start of the old 5th field, to add the new text to the end of the old 5th field, or to replace the contents of the old 5th field with "AVIALABLE "?

Why do you want to use something other than awk for this? Is this a homework assignment that does not allow use of awk? (Oops, ignore that. I see that you have changed your requirements.)

Do you want to add AVAILABLE to the end of the 2nd field as in your code, or add "AVAILABLE " as requested in your description to what was in field 5 before you deleted fields 3 and 4?
# 4  
Old 01-05-2015
Quote:
Originally Posted by RavinderSingh13
Hello Hypesslearner,

Could you please try following and let me know if this helps.
Code:
awk '{$3="AVIALABLE";$4="\b"} 1' OFS="\t" Input_file

Thanks,
R. Singh
Hi Ravinder,
Note that setting the 4th field to a backspace character does not remove the 4th field. Note also that if any of the fields contain spaces between words within the field, you need to set FS to tab as well as setting OFS to tab to get the proper output. For example, the command:
Code:
echo "1 2 3 4 5 6"|awk '{$3="AVIALABLE";$4="\b"} 1' OFS="\t" |od -bc

produces the output:
Code:
0000000   061 011 062 011 101 126 111 101 114 101 102 114 105 011 010 011
           1  \t   2  \t   A   V   I   A   L   A   B   L   E  \t  \b  \t
0000020   065 011 066 012                                                
           5  \t   6  \n                                                
0000024

My interpretation of the request to "remove the column 3 and column 4 and insert a word in column 3" is different than yours. I thought it meant to delete fields 3 and 4 and modify the new 3rd field. You thought it meant to replace field 3 and delete field 4. And the code provided by the OP, added text to the end of the 2nd field, deleted the 3rd and 4th fields, and added a new empty field to the end of each line.

Until we get an unambiguous description of the desired processing, I don't know which interpretation is correct.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Delete an entire column from a tab delimited file

Hi, Can anyone please tell me about how we can delete an entire column from a tab delimited file? Mu input_file.txt looks like this: And I want the output as: I used the below code nawk -v d="1" 'BEGIN{FS=OFS="\t"}{$d=""}{print}' input_file.txtBut in the output, the first column is... (5 Replies)
Discussion started by: sampoorna
5 Replies

3. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

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

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

6. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

7. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

8. Shell Programming and Scripting

Delete first column in tab-delimited text-file

I have a large text-file with tab-delimited genetic data that looks like: KSC112 KSC234 0 0 1 1 A G C T I simply wan to delete the first column, but since the file has 600 000 columns, it is not possible with awk (seems to be limited at 32k columns). Does anyone have an idea how to do this? (2 Replies)
Discussion started by: andmal
2 Replies

9. UNIX for Dummies Questions & Answers

Adding EMPTY columns to Tab-delimited txt file

Hi 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... (8 Replies)
Discussion started by: Banni
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
Login or Register to Ask a Question