Replace blank values for string using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace blank values for string using sed
# 1  
Old 05-31-2010
Replace blank values for string using sed

Hi everyone,

I was wondering how could from a file where each row is separated by tabulations, the row values where are in blank replace them by a string or value.

My file has this form:

Code:
26/01/09	13:45:00	0		0	
26/01/09	14:00:00	1495.601318	0	
26/01/09	14:15:00	1495.601318	0	
26/01/09	14:30:00	1495.601318	0	
26/01/09	14:45:00	1495.601318	0	
26/01/09	15:00:00	1495.601318	0	
26/01/09	15:15:00	1495.601318	0	
26/01/09	15:30:00	1495.601318	0	
26/01/09	15:45:00			0	
26/01/09	16:15:00	1105.571899	0	
26/01/09	16:30:00	1111.437012	0

I've tried using sed command like this:

Code:
sed 's/[:blank:]+/-9999/g' input > output

but it doesn't work, could somebody tell me what I'm doing wrong,

thanks in advance.

Last edited by Scott; 05-31-2010 at 08:39 AM.. Reason: Code tags, please...
# 2  
Old 05-31-2010
Try...
Code:
sed 's/^$/-9999/' input > output

# 3  
Old 05-31-2010
Try:
Code:
awk -F\t '{if($3=="")$3=string}1' OFS="\t" string=x file


Last edited by danmero; 05-31-2010 at 12:53 PM.. Reason: Change logic
# 4  
Old 05-31-2010
tonet,
can you please put your input in code tags for a better readability?

If it's tab delimited, than it's something like this:
Code:
26/01/09	13:45:00	0		0 
26/01/09	14:00:00	1495.601318	0 
26/01/09	14:15:00	1495.601318	0 
26/01/09	15:45:00			0 
26/01/09	16:15:00	1105.571899	0

Right?

Now you want it to be as following?
Code:
26/01/09	13:45:00	0		0 
26/01/09	14:00:00	1495.601318	0 
26/01/09	14:15:00	1495.601318	0 
26/01/09	15:45:00	-9999		0 
26/01/09	16:15:00	1105.571899	0

Right?
# 5  
Old 05-31-2010
Quote:
Originally Posted by tonet
Code:
sed 's/[:blank:]+/-9999/g' input > output

Code:
sed 's/\t\t\t/\t-9999\t/g' input > output

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

5. Shell Programming and Scripting

Replace comma with a blank space using SED

Hello everyone, I want to replace all "," (commas) with a blank space My command thus far is: cat test.text | sed -e s/\`//g | awk '{print$1" "$2" "$3}' I'm sure you guys know this, but the SED command that I am using is to get rid of the "`" (tics). which gives me: name ... (5 Replies)
Discussion started by: jayT
5 Replies

6. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. UNIX for Dummies Questions & Answers

using sed to replace values...

I have a file: $somevar=somevalue $anothervar= $someothervar=45 I'd like to be able to replace the values in the file. I just don't know how exactly to use sed. I was thinking along the lines of: cat file | sed "s/$somevar=/anotherval/g" I was hoping this would make the... (2 Replies)
Discussion started by: mrwatkin
2 Replies

9. Shell Programming and Scripting

How do i use sed to replace string with values from a dictionary file

I have file 1 with one million rows. one of the fields is "FIRSTNAME" (the string) I have a second file with about 20 first names. JUDE DAVID HOMER CANE ABEL MARTY CARL SONNY STEVE BERT OSCAR MICKY JAMES JOHN GLENN DOUG (3 Replies)
Discussion started by: yoyolandre
3 Replies
Login or Register to Ask a Question