replacing spaces with null or 0 in the input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing spaces with null or 0 in the input file
# 1  
Old 01-06-2009
replacing spaces with null or 0 in the input file

hi
i have records in my input file like this

aaa|1234||2bc||rahul|tamilnadu
bba|2234||b4c||bajaj|tamilnadu


what i am expecting is in between two pipes if there is no character it should be replaced with null or 0

so my file will look like this

aaa|1234|null|2bc|0|rahul|tamilnadu
bba|2234|null|b4c|0|bajaj|tamilnadu


how to do it

can anyone explain

thanks in advance
# 2  
Old 01-06-2009
Code:
$ cat tri.txt
aaa|1234||2bc||rahul|tamilnadu
bba|2234||b4c||bajaj|tamilnadu

$ awk 'BEGIN{OFS=FS="|"}{ for(k=0;k<=NF;k++)
if ( $k == "" )
$k="NULL"
print $0}' tri.txt

aaa|1234|NULL|2bc|NULL|rahul|tamilnadu
bba|2234|NULL|b4c|NULL|bajaj|tamilnadu

# 3  
Old 01-06-2009
Code:
sed -e 's/||/|null|/g' -e 's/||/|null|/g' FILE

# 4  
Old 01-06-2009
thanks a lot it worked
but i wanted null and 0 to be populated
is there any way for the same

thanks in advance
# 5  
Old 01-06-2009
Code:
#! /usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	my @tmp=split("[|]",$_);
	map{$_=$_||"null"} @tmp;
	print join "|",@tmp;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Replacing multiple spaces in flat file

Greetings all I have a delimited text file (the delimiter is ';') where certain fields consist of many blanks e.g. ; ; and ; ; Before I separate the data I need to eliminate these blanks altogether. I tried the sed command using the following syntax: sed -i 's/; *;/;;/g' <filename> ... (15 Replies)
Discussion started by: S. BASU
15 Replies

3. Shell Programming and Scripting

Select command with variable options having spaces or null contents

Hi, I'm having an issue trying to produce a hierarchical directory menu that has either any directories listed in a specific directory as options or options with spaces in the option content or null content. So the menu function gets passed a base directory, it then lists any .sh scripts in... (6 Replies)
Discussion started by: andyatit
6 Replies

4. Shell Programming and Scripting

Awk replacing file with user input

this section of the awk code i have here takes file to work with from the user. the user specifies the file name from the command line and the file name is assigned to the variable $FLIST awk 'BEGIN { while((getline < "'${FLIST}'")>0) S FS="\n"; RS="}\n" } now, i dont want... (5 Replies)
Discussion started by: SkySmart
5 Replies

5. UNIX for Dummies Questions & Answers

Replacing part of a text file with user input.

Ok, I am brand new to UNIX and I am trying to learn a cross between basic script and database use. I had got some ideas off the net on simple ideas for learning UNIX. I am working on creating a simple phone book program that allows myself to enter our employees from work into a phone book text... (0 Replies)
Discussion started by: georgefurbee
0 Replies

6. Shell Programming and Scripting

awk{FIELDWIDTHS} replacing blanks with null

Hi, I am having a file and grabbed the contents of the field according to field widths. The command i used is: awk 'BEGIN{FIELDWIDTHS="10 25 20 14 6 10"}{print$4,$5,$6}' newtext.text >test1.txt i got the output for example: val1 val2 val3 <blank> ... (3 Replies)
Discussion started by: rish_max
3 Replies

7. Shell Programming and Scripting

Replacing data of output file with input

Hi, I have a ksh which peocess and get me data from 3 days... ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out Now time.out cannot have deplicate data so what i want... (10 Replies)
Discussion started by: bhagya2340
10 Replies

8. Shell Programming and Scripting

need help in replacing spaces in a file

hi all this is the part i am facing a problem eg data: filename : tr1 + T 40 this is a sample record in that file ... the value of T can be anything, but will be a single character. i need to cut from field two, and i am using this command cut -d " " -f2 tr1 >tr3 and the o/p is ... (7 Replies)
Discussion started by: sais
7 Replies

9. Shell Programming and Scripting

Handling null input...

Ok, so when a user inputs nothing, simply pressing enter when prompted for a phone number, I get a "./addrbkfct.sh: test: argument expected" error message. I have the following function: addNumber(){ echo "Phone number: \c"; read number; echo $number; if ;... (2 Replies)
Discussion started by: DrRo183
2 Replies

10. Solaris

finding & replacing blank rows/spaces in a file

Can anyone help me find and replace blank rows in a file with a numeric value (ie blankrow=someTxtOrNumValue), the file is over 500,000 rows long so it would need to be the quickest way as I'll need to do this for multiple files...I would be greatfull for any suggestions....thanks sample file:... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question