How to replace the blank in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace the blank in unix
# 1  
Old 09-17-2010
How to replace the blank in unix

Hi,

I have data like below: The columns MARKET ,STORE, WEEK1 will be present always. After that, there can be week2, week3 columns or some times there wont be no columns after week1. The week1 or week2 columns can have data or blank. I need to replace the blank with NIS.

Code:
MARKET,STORE,STORE NAME,WEEK1,WEEK2
MARKET,STORE1,STORENAM1, 000000003.99,
MARKET,STORE1,STORENAM2,NH,NH
MARKET,STORE1,STORENAM3, 000000003.99, 000000003.99
MARKET,STORE1,STORENAM4,,NH
MARKET,STORE1,STORENAM5,, 000000003.99
MARKET,STORE1,STORENAM6,, 000000008.60

Expected output:
Code:
MARKET,STORE,STORE NAME,WEEK1,WEEK2
MARKET,STORE1,STORENAM1, 000000003.99,NIS
MARKET,STORE1,STORENAM2,NH,NH
MARKET,STORE1,STORENAM3, 000000003.99, 000000003.99
MARKET,STORE1,STORENAM4,NIS,NH
MARKET,STORE1,STORENAM5,NIS, 000000003.99
MARKET,STORE1,STORENAM6,NIS, 000000008.60

I hope i am making you clear. if not, please let me know.

Thanks,
Selva

Last edited by Scott; 09-17-2010 at 01:18 PM.. Reason: Code tags, please...
# 2  
Old 09-17-2010
Code:
sed -e 's/,, */,NIS, /g' -e 's/\(, .*,$\)/\1 NIS/g' infile

# 3  
Old 09-17-2010
Hi,

Thanks for your reply. but it is not working if my input data is like below. And could you please explain the code?

MARKET,STORE,STORENAME1,NH,

Thanks,
Selva
# 4  
Old 09-17-2010
Code:
$ awk -F, '$4 == "" {print $1 "," $2 "," $3 ",NIS," $5} $4 != "" {print $0}' <<INPUT
> MARKET,STORE,STORE NAME,WEEK1,WEEK2
> MARKET,STORE1,STORENAM1, 000000003.99,
> MARKET,STORE1,STORENAM2,NH,NH
> MARKET,STORE1,STORENAM3, 000000003.99, 000000003.99
> MARKET,STORE1,STORENAM4,,NH
> MARKET,STORE1,STORENAM5,, 000000003.99
> MARKET,STORE1,STORENAM6,, 000000008.60
> INPUT
MARKET,STORE,STORE NAME,WEEK1,WEEK2
MARKET,STORE1,STORENAM1, 000000003.99,
MARKET,STORE1,STORENAM2,NH,NH
MARKET,STORE1,STORENAM3, 000000003.99, 000000003.99
MARKET,STORE1,STORENAM4,NIS,NH
MARKET,STORE1,STORENAM5,NIS, 000000003.99
MARKET,STORE1,STORENAM6,NIS, 000000008.60
$

# 5  
Old 09-17-2010
Code:
sed -e 's/,, */,NIS, /g' -e 's/\(,$\)/\1 NIS/g' infile


Quote:
could you please explain the code?
Code:
sed -e 's/,, */,NIS, /g' -e 's/\(,$\)/\1 NIS/g' infile


Find a double comma
Followed by zero or more blanks
And substitute them with the string ",NIS, "
Match all occurences in a line
Combine multiple sed commands
Find a comma at the end of a line
Save the matched pattern
And substitute it with the pattern followed by the string " NIS"

Last edited by verdepollo; 09-17-2010 at 04:53 PM..
# 6  
Old 09-17-2010
Code:
$ sed -e 's/, *,/,NIS,/g' -e 's/, *$/,NIS/g' <<INPUT
> MARKET,STORE,STORE NAME,WEEK1,WEEK2
> MARKET,STORE1,STORENAM1, 000000003.99,
> MARKET,STORE1,STORENAM2,NH,NH
> MARKET,STORE1,STORENAM3, 000000003.99, 000000003.99
> MARKET,STORE1,STORENAM4,,NH
> MARKET,STORE1,STORENAM5,, 000000003.99
> MARKET,STORE1,STORENAM6,, 000000008.60
> INPUT
MARKET,STORE,STORE NAME,WEEK1,WEEK2
MARKET,STORE1,STORENAM1, 000000003.99,NIS
MARKET,STORE1,STORENAM2,NH,NH
MARKET,STORE1,STORENAM3, 000000003.99, 000000003.99
MARKET,STORE1,STORENAM4,NIS,NH
MARKET,STORE1,STORENAM5,NIS, 000000003.99
MARKET,STORE1,STORENAM6,NIS, 000000008.60
$


Last edited by fnds; 09-17-2010 at 05:01 PM.. Reason: fix an error
# 7  
Old 09-17-2010
Hi,

It is working as expected. I really thank you.

Thanks,
Selva
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

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: 2015/08/04 00:00:00 171 730579 27088 <blank> 3823 30273 1621778 ... (6 Replies)
Discussion started by: amyt1234
6 Replies

3. UNIX for Dummies Questions & Answers

Replace character by blank

Hi all, I have 89 columns,1200 rows in a flat file, some of the values are just '.' (the character dot). I want to replace them by nothing (blank), but when I do so, it affects the decimal numbers too. so 12.34 becomes 1234. How can I just replace values which are only '.' with 1 white... (13 Replies)
Discussion started by: newbie83
13 Replies

4. Shell Programming and Scripting

Replace with NA if blank is there

Hi All, please help me below request. i am having a file XXXX.dat. in Unix. example- plan_dk,imsmodelvar,paymt_type_az,mmars_plan,mmars_payer,mmars_pbm,account,botpbm,formulary_Crestor 0000000000,,3,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A, 0000000000,6,,MAIL ORDER,MAIL ORDER... (10 Replies)
Discussion started by: krupasindhu18
10 Replies

5. Shell Programming and Scripting

Need help to replace a pattern with a blank line

Need help to replace the line beginning with tcp_sendspace with a blank line. # cat if en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN> inet 10.27.53.21 netmask 0xffffff00 broadcast 10.207.52.255 inet... (11 Replies)
Discussion started by: sags007_99
11 Replies

6. UNIX for Dummies Questions & Answers

Replace colon with blank space

Dear Gurus, I have a unix file with multiple colons on each row, and I would like to replace each colon with a blank space using the awk command. For example, I have the following data: Data: --------- A~000000000000518000~SLP:~99991231~20090701~00102.00~USD:~CS:~... (2 Replies)
Discussion started by: chumsky
2 Replies

7. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

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

9. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

10. AIX

How can i replace a character with blank space?

i want a command for my script!!! say file consists of character 123 125 127. i need a query to replace the number 2 with 0 so the output should be 103 105 107. i use unix-aix (8 Replies)
Discussion started by: rollthecoin
8 Replies
Login or Register to Ask a Question