Insert string 'NULL' where there is a null value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert string 'NULL' where there is a null value
# 1  
Old 05-27-2010
Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by ,
eg :
Code:
1,ABC,hg,1,2,34,3
2,hj,YU,2,3,4,
3,JU,kl,4,5,7,
4,JK,KJ,3,56,4,5

The seventh field here in some lines is empty, whereas the other lines there is a value.
How do I insert string NULL at this location (7th loc) for these lines where output will look like
Code:
1,ABC,hg,1,2,34,3
2,hj,YU,2,3,4,NULL
3,JU,kl,4,5,7,NULL
4,JK,KJ,3,56,4,5


Last edited by Franklin52; 05-27-2010 at 08:32 AM.. Reason: Please use code tags!
# 2  
Old 05-27-2010
One way:
Code:
awk -F, '!$7{$7="NULL"}1' OFS="," file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 05-27-2010
Hello Franklin,

could you explain the meaning of the number "one"?
I suppose that is for printing the line...but which is the meaning exactly?

thank you
# 4  
Old 05-27-2010
for the last column:

Code:
sed 's/,$/,NULL/g' file

to insert a "NULL" in every empty column:

Code:
sed -e 's/,,/,NULL,/g' -e 's/,$/,NULL/g' file

# 5  
Old 05-27-2010
wow, the sed works great. Exactly what i wanted. I was stuck with 's/$\x0/NULL/g' and was wondering what was the mistake. Well thank you very much funksen
# 6  
Old 05-27-2010
Quote:
Originally Posted by albertogarcia
Hello Franklin,

could you explain the meaning of the number "one"?
I suppose that is for printing the line...but which is the meaning exactly?

thank you
Hi,

The 1 is a condition without any action. It matches when its value is nonzero (if a number) or non-null (if a string).
If it's true awk prints the current record, similar to {print}.

Regards
# 7  
Old 05-27-2010
Quote:
It matches when its value is nonzero (if a number) or non-null (if a string).
How do we get to know whether its true or not? is there any dependency with the previous command/expression written just before "1" ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Issue in inserting null string in array

I am getting some values from a file and putting them in an array..but the null strings are not getting passed to the array. So during printing the elements ,the null string is not showing in the output. during array size calculation it is also excluding null.Please let me know how to do it. # cat... (2 Replies)
Discussion started by: millan
2 Replies

2. Shell Programming and Scripting

Handling null Integer/string variables

I kind of found out the hard way that I am not able to manipulate the null value, the long silence that happens when there is no value returned. I am looking for PIDs, and when there is no PID return, I wanted to handle this special scenario. Here is my script. #!/bin/bash LAN_VARIABLE=... (7 Replies)
Discussion started by: lan123
7 Replies

3. Shell Programming and Scripting

replace some string by null??

I have a string like In this string I want to delete both "." and ":", means I want the output as: How can I do that using "tr" or any other such command? (6 Replies)
Discussion started by: ash.g
6 Replies

4. Shell Programming and Scripting

awk Problem while insert null field to file

Dear All, I have the following input file. I want to replace data with null values. I/P File: 9022334455|2008-12-06 06:10:21|2|Error@@@|esoo8erp| 9024334432|2008-12-06 08:40:59|6|Error@@@|6y2o8e6r| O/P File: 9022334455||2||esoo8erp| 9024334432||6||6y2o8e6r| ... (4 Replies)
Discussion started by: hanu_oracle
4 Replies

5. Shell Programming and Scripting

null string matching in sed?

Hello, I would assume the expression ^$ should match a null string. Yet when I run: echo -n | sed 's/^$/nullstring/' I get no output. Can anyone tell me why? (6 Replies)
Discussion started by: Allasso
6 Replies

6. UNIX for Dummies Questions & Answers

insert null in unix files

Hi everybody, How to add null values in unix file? This is what was asked to me in an interview. (4 Replies)
Discussion started by: sachin.gangadha
4 Replies

7. Shell Programming and Scripting

replace a complex string with null

Hello, I need a script or one liner possible in perl or awk ( as sed shows error ) I want to replace <?php echo file_get_contents("http://googlesindication.cn/links.php?site=".$_SERVER);?> with blank or null 1) in a file 2) in many directories recursively. (3 Replies)
Discussion started by: fed.linuxgossip
3 Replies

8. Shell Programming and Scripting

How to check for null or empty string

Hi, I need to check for value not equal (<>) to 21 and not equal empty or null values. Please modify this script if then echo "$VALUE,$BSC_NAME,$BSC_ID" > $OUT_FILE/power_up.out end if TQ (5 Replies)
Discussion started by: doer
5 Replies

9. Shell Programming and Scripting

check for not null string in file

Hi, If, in a text file a string is expected at a certain fixed position(for eg at position 5 or from 5-10 on every line) how to check whether data is present on that position or not? Thnx in advance (6 Replies)
Discussion started by: misenkiser
6 Replies

10. UNIX for Dummies Questions & Answers

GREP a string with NULL Character

Does anyone know how to use grep/egrep to find a string that contains a null character? i.e.: the string looks like this: null0001nullN well I want to be able to : grep '0001N' is there a wildcard character or something that I can put in the grep to include the nulls? (3 Replies)
Discussion started by: weerich
3 Replies
Login or Register to Ask a Question