How to remove empty field in a text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove empty field in a text file?
# 1  
Old 01-14-2015
How to remove empty field in a text file?

Hi all,

I want to remove empty field in a text file. I tried to used sed. But it failed.

Input:
Code:
LG10_PM_map_19_LEnd	1000560	G	AG	AG				
LG10_PM_map_19_LEnd	1005621	G		AG				
LG10_PM_map_19_LEnd	1011214	A	AG	AG				
LG10_PM_map_19_LEnd	1011673	T	CT	CT				
LG10_PM_map_19_LEnd	1088961	C						CT
LG10_PM_map_19_LEnd	1089024	G						AG
LG10_PM_map_19_LEnd	1108301	C						
LG10_PM_map_19_LEnd	11365128	G		AG	AG	AG	AG	AG
LG10_PM_map_19_LEnd	11365170	T		CT	CT	CT	CT	CT
LG10_PM_map_19_LEnd	11381744	A			AG	AG	AG	AG
LG10_PM_map_19_LEnd	11381772	T				CT	CT	CT
LG10_PM_map_19_LEnd	11385851	A			AG		AG	AG
LG10_PM_map_19_LEnd	11386265	A			AC		AC	AC
LG10_PM_map_19_LEnd	1138663	T						AT

Output
Code:
LG10_PM_map_19_LEnd	1000560	G	AG	AG				
LG10_PM_map_19_LEnd	1005621	G	AG				
LG10_PM_map_19_LEnd	1011214	A	AG	AG				
LG10_PM_map_19_LEnd	1011673	T	CT	CT				
LG10_PM_map_19_LEnd	1088961	C	CT
LG10_PM_map_19_LEnd	1089024	G	AG
LG10_PM_map_19_LEnd	1108301	C		
LG10_PM_map_19_LEnd	11365128	G	AG	AG	AG	AG	AG
LG10_PM_map_19_LEnd	11365170	T	CT	CT	CT	CT	CT
LG10_PM_map_19_LEnd	11381744	A	AG	AG	AG	AG
LG10_PM_map_19_LEnd	11381772	T	CT	CT	CT
LG10_PM_map_19_LEnd	11385851	A	AG	AG	AG
LG10_PM_map_19_LEnd	11386265	A	AC	AC	AC
LG10_PM_map_19_LEnd	1138663	T	AT

How can remove these blank fields?

Thanks in advance.
# 2  
Old 01-14-2015
Hello huiyee1,

Could you please try following, it may help you.
Code:
awk '{$1=$1} 1' OFS="\t" Input_file

Output will be as follows.
Code:
LG10_PM_map_19_LEnd     1000560 G       AG      AG
LG10_PM_map_19_LEnd     1005621 G       AG
LG10_PM_map_19_LEnd     1011214 A       AG      AG
LG10_PM_map_19_LEnd     1011673 T       CT      CT
LG10_PM_map_19_LEnd     1088961 C       CT
LG10_PM_map_19_LEnd     1089024 G       AG
LG10_PM_map_19_LEnd     1108301 C
LG10_PM_map_19_LEnd     11365128        G       AG      AG      AG      AG      AG
LG10_PM_map_19_LEnd     11365170        T       CT      CT      CT      CT      CT
LG10_PM_map_19_LEnd     11381744        A       AG      AG      AG      AG
LG10_PM_map_19_LEnd     11381772        T       CT      CT      CT
LG10_PM_map_19_LEnd     11385851        A       AG      AG      AG
LG10_PM_map_19_LEnd     11386265        A       AC      AC      AC
LG10_PM_map_19_LEnd     1138663 T       AT

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-14-2015
Try:
Code:
tr -s '\t' < file



--
Note: RavinferSing13's suggestion works also, as long as there are no spaces somewhere in the input file, only TABS
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-14-2015
Thanks everyone.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command to extract empty field in a large UNIX file?

Hi All, I have records in unix file like below. In this file, we have empty fields from 4th Column to 22nd Column. I have some 200000 records in a file. I want to extract records only which have empty fields from 4th field to 22nd filed. This file is comma separated file. what is the unix... (2 Replies)
Discussion started by: rakeshp
2 Replies

2. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

4. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

5. Shell Programming and Scripting

awk - remove row if specific field is empty/blank

I have this text.filecharles darwin sam delight george washington johnson culper darwin sam delight micheal jackson penny lite and would like to remove the row, if the first field is blank. so the result would be: result.filecharles darwin sam ... (4 Replies)
Discussion started by: charles33
4 Replies

6. Shell Programming and Scripting

Format the file by deleting empty field

I have the test data with 10 column separated by comma and each column has more than 1000000 rows. Can anyone help me to find empty field in all columns and delete that empty field alone and lift that column up by one row. Data with empty field: A74203XYZ,A21718XYZ,A72011XYZ,A41095XYZ,... (7 Replies)
Discussion started by: zooby
7 Replies

7. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

8. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

9. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

10. Shell Programming and Scripting

Awk scrip to remove empty field

Hi I have a file which looks like this name: Sally group: Group4 name: Tim group: Group1 name: Dan group: Group2 name: Chris group: Group3 name: Peter group: name: Fred group: name: Mary group: Group2 Well I want to get rid of the... (4 Replies)
Discussion started by: bombcan
4 Replies
Login or Register to Ask a Question