How to replace blank tab with zero in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace blank tab with zero in a file?
# 1  
Old 08-04-2015
How to replace blank tab with zero in a file?

hi,

i need to replace a blank tab output in a file to zero.

input file:
Code:
2015/08/04 00:00:00                       171                       730579                         27088                 <blank>                             3823                          30273                  1621778             3284136                        284200                      1267655                418709                        1320811                         51865           <blank>                                                    198387                            518


output:
Code:
2015/08/04 00:00:00                       171                       730579                         27088                 0                             3823                          30273                  1621778             3284136                        284200                      1267655                418709                        1320811                         51865           0                                                    198387                            518

# 2  
Old 08-04-2015
I guess you don't want sed 's/<blank>/0/g' file; then you should supply more details, e.g. is that a <TAB> delimited file? Are all fields except the first one(or two?) numeric? Any spaces in there?
# 3  
Old 08-04-2015
Yes all the fields are numeric except first one which is date.

the file is tab delimited and actually a output from sql query.

some of the rows are null so u see more space between column where it is null and i need to replace those blank null tab will zero.
# 4  
Old 08-04-2015
Then try
Code:
awk -F"\t" '{for (i=2;i<=NF;i++) if (!$i) $i=0}1' OFS="\t" file

# 5  
Old 08-04-2015
Sorry its not working.



Code:
awk -F"\t" '{for (i=2;i<=NF;i++) if (!$i) $i=0}1' OFS="\t" test

output
Code:
2015/08/04 00:00:00                       171                       730579                         27088                                              3823                          30273                  1621778             3284136                        284200                      1267655                418709                        1320811                         51865                                                                                198387                            518

# 6  
Old 08-04-2015
Code:
 awk -F"\t" '1; {for (i=2;i<=NF;i++) if (!$i) $i=0}1' OFS="\t" file
2015/08/04 00:00:00    171    730579    27088         3823    30273    1621778    3284136    284200    1267655    418709    1320811    51865         198387    518
2015/08/04 00:00:00    171    730579    27088    0    3823    30273    1621778    3284136    284200    1267655    418709    1320811    51865    0    198387    518

---------- Post updated at 19:44 ---------- Previous update was at 19:42 ----------

Is it possible that "\t" doesn't work for FS and OFS on your system? Try inserting it literally.
# 7  
Old 08-04-2015
Are you in Solaris? If so use /usr/xpg4/bin/awk rather than awk.
Is you file in fact TAB delimited? The sample you posted is not.


--
Quote:
Originally Posted by RudiC
[..]
Is it possible that "\t" doesn't work for FS and OFS on your system? Try inserting it literally.
Every awk should be able to understand "\t".

Code:
[..] if (!$i) [..]

Why not just
Code:
if($i=="")

than you can be sure Awk will treat it as an empty string.

Last edited by Scrutinizer; 08-05-2015 at 03:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

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

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

3. Shell Programming and Scripting

[bash] - Replace blank and string in csv file

Hi all, i have a .csv file with only two columns, like: Login;Status Luca;S Marco; Stefano; Elettra;S Laura; ... I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like: Login;Status Luca;Disabled Marco;Enabled Stefano;Enabled... (10 Replies)
Discussion started by: kamose
10 Replies

4. UNIX for Dummies Questions & Answers

Replace period in a tab delimited file to a number

I have a file like this. It is tab delimited. Unfortunately, the missing data was filled in with a period "." (see the leading lines 1-5 columns) I want to substitute the periods for misisng data with an integer "-999". however, I do not want the global replace to change the other periods seen... (7 Replies)
Discussion started by: genehunter
7 Replies

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

6. Shell Programming and Scripting

How to replace & with and in tab separated file?

Hi, I have a tab separated. I want to replace all the "&" in 8th column of the file with "and" .I am trying with awk -F, -vOFS=\\t '{$8=($8=="&")?"and":$8}1' test> test1.txt My file is abc def ghk hjk lkm hgb jkluy acvf & bhj hihuhu fgg me mine he her go went has has & had hgf hgy ... (1 Reply)
Discussion started by: jagdishrout
1 Replies

7. Shell Programming and Scripting

how to replace a character with blank in a file

hi, I have a doubt in replacing characters with blank. My requirement is that, i have one file and looks like below 4:ALTER SYSTEM DISCONNECT SESSION '193,191' IMMEDIATE; 6:ALTER SYSTEM DISCONNECT SESSION '205,7274' IMMEDIATE; 5:ALTER SYSTEM DISCONNECT SESSION '206,34158' IMMEDIATE;... (4 Replies)
Discussion started by: sridhusha
4 Replies

8. Shell Programming and Scripting

Replace blank spaces with semicolon - text file

Hi all, Been trying to find a solution to this, I'm sure its a sed 1 liner, but I don't know sed well enough to work it out... I have a text file in the following format: 431 666 1332 2665 0.24395 432 670 ... (3 Replies)
Discussion started by: mpcengineering
3 Replies

9. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

10. Shell Programming and Scripting

Replace blank spaces by single tab, and right alignment

Folks, I am wondering if anyone solve this problem. What I want to know is, 1. Delete all white spaces including leading blank space in each line (e.g. line 2), and replace such spaces by single tab except leading blank space 2. Then, align all columns to the right. But, output white space... (1 Reply)
Discussion started by: Jae
1 Replies
Login or Register to Ask a Question