sed - insert text if column empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - insert text if column empty
# 1  
Old 09-13-2012
sed - insert text if column empty

Hi,

I want to insert the text 'Unknown' in 2 specific columns in a csv file (actually | separated) if the column is blank. Its always the same columns.

I have tried using sed: sed "s/||/|Unknown|/g"

but there are occasion where other fields are blank and they need to be left blank. This command inserts Unknowns in all blank fields.

Any help much appreciated
Thanks
Kieran
# 2  
Old 09-13-2012
checking columns 3 and 5
Code:
awk -F='|' '$3=="" {$3="Unknown"}; $5=="" {$5="Unknown"};1' OFS='|' myFile

# 3  
Old 09-14-2012
Quote:
Originally Posted by ksexton
I have tried using sed: sed "s/||/|Unknown|/g"
The "g" at the end replaces ALL occurrences, therefore you had all those false positives. You can put a number there to replace only the n-th occurrence of the match. "1" is implied if you omit the number, therefore

Code:
sed "s/||/|Unknown|/"

will only change the first occurrence. As your file is probably record-oriented this might work.

If it doesn't work you will have to extend your regexp to match only the one field you want to change. A field looks like: "any number (including 0 if the field is empty) of non-'|' followed by a '|'". The regexp for a single field is:

Code:
/[^|]*|/

Now repeat this n-1 times and add the n-th field you are looking for empty. We will have to group with "\(...\)" and use the regexp multiplier "\{...\}". After this we will need an additional grouping because we need to preserve the beginning of the line unchanged.

For instance, if n=6:

Code:
sed '/^\(\([^|]*|\)\{5\}\)||/\1|Unknown|/'

I hope this helps.

bakunin
# 4  
Old 09-14-2012
Code:
sed 's/[|] *[|]/|Unknown|/g'


Last edited by Franklin52; 09-17-2012 at 03:29 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 09-14-2012
Quote:
Originally Posted by ksexton
I have tried using sed: sed "s/||/|Unknown|/g"
Quote:
Originally Posted by rdrtx1
sed 's/[|][|]/|Unknown|/g'
I'm curious. Why do you believe that your suggestion will behave differently from what was already tried?

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I insert text with sed ?

Hi I was wondering if anyone new of a solution to this problem? I need to copy a time stamp that is on a line of .text in a text file into multiple positions on the same line. I need to insert the time stamp on the same line between every occurance of the text ".pdf_.html" right after the... (9 Replies)
Discussion started by: Paul Walker
9 Replies

2. Shell Programming and Scripting

Insert text into specific column

I have the following data: Dec 24 11:31:10 0000008b 9911662486 Answered Price SGD 0.003 PERIOD: 0 m 6 s Dec 24 11:21:42 00000086 9911662486 Answered Price SGD 0.001 PERIOD: 0 m 2 s Dec 20 15:34:28 00000004 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 12 s Dec 20 18:42:30 0000017b... (6 Replies)
Discussion started by: alegnagrp
6 Replies

3. Shell Programming and Scripting

insert text into empty file

I have an awk script to extract data from several files and create output in the following format as a csv file: xxxx 01/04/12 0001 0 When data is present, I have a file. When no data is available in the input files, I would still like to create a file that looks like this: xxxx... (1 Reply)
Discussion started by: banjo25
1 Replies

4. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

5. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

6. Shell Programming and Scripting

Can sed be used to insert data at specific column?

I'm trying to use sed to insert data at a specific column, let's say my data looks like this: 0553 1828 0552 1829 0550 1829 0549 1830 0548 1831 what I want is this: timein 0553 timeout 1828 timein 0552 timeout 1829 timein 0550 timeout 1829 timein 0549 timeout 1830 timein 0548... (5 Replies)
Discussion started by: mswartz
5 Replies

7. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

8. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

9. Shell Programming and Scripting

insert text into column

Hello! I have a text file containing some text : : : bla other text : : : bla any text : : : bla containing 3 columns separated by the ':' sign. Now i want to insert 'this' in the 2nd line after the 2nd column-deliminiter (i.e. into 3rd column), like some text : : : bla other text :... (1 Reply)
Discussion started by: knoxo
1 Replies

10. UNIX for Dummies Questions & Answers

Insert Text With Sed

Hello. Trying to insert text at line 1 and after last line of file. I have searched posts but nothing seems to work. I keep getting extra characters error or nothing gets inserted into the file. #!/bin/sh touch textfile.txt sed 'i\ Add this line before every line with WORD' textfile.txt ... (5 Replies)
Discussion started by: steveramsey
5 Replies
Login or Register to Ask a Question