replacing characters with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing characters with awk
# 1  
Old 03-16-2009
replacing characters with awk

Hi there!

I'm new in this and probably is a quite simple task but I still cannot manage to do it:

I have to read some files line by line and then change the input format into another one, but the very first step is to replace the empty variables by error values. I mean, each line looks like:

123.45,rank,bla1,,wwer334,464,2234l20,,,,,

1 2 3 4 5 6 7 8 9 10 11 12

(the first line is the 'actual' input line while the other one indicates the column# or variable# )

the output should be like:

123.45,rank,bla1,-9999,wwer334,464,2234l20,-9999,-9999,-9999,-9999,-9999

1 2 3 4 5 6 7 8 9 10 11 12


Well, this is what I have written to try to do it (I forgot telling, my scripts are in bash):

#!/bin/sh
#--------------
cat input | awk -F ',' ' function fillit(num) {if (num != "") print num; else print -9999} {print "$1="$1,"$2="$2,"$3="$3,"$4="fillit($4),"$5="$5,"$6="$6,"$7="$7,"$8="fillit($8),"$9="fillit($9),"$10 ="fillit($10),"$11="fillit($11),"$12="fillit($12)} '
#
#--------------

I defined a function called fillit() that subsitutes a blank variable by an error value. And the " " commentaries before each variable are just to help me knowing what was going on...

The problem is that the output looks like:

-9999
-9999
-9999
-9999
-9999
-9999
$1=123.45 $2=rank $3=bla1 $4= $5=wwer334 $6=464 $7=2234l20 $8= $9= $10= $11= $12=


While I was expecting something like:

$1=123.45 $2=rank $3=bla1 $4=-9999 $5=wwer334 $6=464 $7=2234l20 $8=-9999 $9=-9999 $10=-9999 $11=-9999 $12=-9999

That is, it actually substitutes the variables, but instead of putting them in the propper order, it puts them in a column at the beginning .

Well, thank you very much in advance for your help ;¬).
# 2  
Old 03-16-2009
Code:
echo '123.45,rank,bla1,,wwer334,464,2234l20,,,,,' | nawk -F, '{for(i=1;i<=NF;i++) if ($i == "") $i="-9999"}1' OFS=','

# 3  
Old 03-16-2009
Wow,

Thanks a lot! It really helped me!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Replacing Hex Characters In A File Using awk?

Hi guys, First off, i'm a complete noob to UNIX and LINUX so apologies if I don't understand the basics! I have a file which contains a hex value of '0D' at the end of each line when I look at it in a hex viewer. I need to change it so it contains a hex value of '0D0A0A' I thought... (10 Replies)
Discussion started by: AndyBSG
10 Replies

2. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

3. UNIX for Dummies Questions & Answers

Replacing multiple characters

I have a file as follows ggsnApnDownlinkBytes.X ggsnApnDownlinkDrops.X ggsnApnDownlinkPackets.X ggsnApnName.X ggsnApnUplinkBytes.X ggsnApnUplinkDrops.X ggsnApnUplinkPackets.X ggsnApnDownlinkBytes.Y ggsnApnDownlinkDrops.Y ggsnApnDownlinkPackets.Y ggsnApnName.Y ggsnApnUplinkBytes.Y... (7 Replies)
Discussion started by: rob171171
7 Replies

4. Shell Programming and Scripting

Replacing characters

Hi fellow experts, I have a question for you. Data looks like: 00877,05/13/2010,PBO,P,0000708331,518 00877,05/13/2010,PBO,P,0000708331,519 ... ... 00877,05/13/2010,PBO,P,0000708331,2103 00877,05/13/2010,PBO,P,0000708331,2104,etc,etc Basically I have to replace 518,519,2103,2104,... (4 Replies)
Discussion started by: Devski123
4 Replies

5. Shell Programming and Scripting

Replacing strang characters ^Y ^Z ^Q ^A

hello frends, i am facing a problem where when i use vi to ciew my files then every line contains a special character like ^Q ^A ^Y ^Z in the very beginning. i tried to remove them using many techniques already posted in this forum but in vain. please take a sample of file when viewed in vi editor:... (6 Replies)
Discussion started by: vikas_kesarwani
6 Replies

6. Shell Programming and Scripting

Replacing Characters with |

Hi All, example data.log 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 I want like this to 526569346|66815531961|09 526569346|66815531961|09... (4 Replies)
Discussion started by: ooilinlove
4 Replies

7. Shell Programming and Scripting

Replacing Characters

Hi All, I have a file which is delimeted with the character '. i need to replace this character with the same character and also a new line. Can anyone please help me with the tr command for this. Many thanks Karan (11 Replies)
Discussion started by: karansachdeva
11 Replies

8. Shell Programming and Scripting

replacing characters

hi all I have a file that has sone spaces in start then / at last. i want to get rid of this. how to do? eg. 11414/ 49878/ 27627/ I WANT THE FILE AS 11414 49878 27627 PLEASE HELP (3 Replies)
Discussion started by: infyanurag
3 Replies

9. UNIX for Dummies Questions & Answers

replacing characters

Hi, I have a script for replacing bad characters in filenames for f in *; do mv $f `echo $f | tr '+' '_'` done; this replaces + for _ But I need to replace all bad characters ? / % + to _ Pls how can i do this in one script ? (3 Replies)
Discussion started by: palmer18
3 Replies

10. UNIX for Dummies Questions & Answers

replacing few characters in a file

Hi All, I have huge xml file. The file contains some comment tags . I have requirement to replace comment tag with another comment tag. Say for example : file X has -- Part of the file <?xml version="1.0" encoding="ISO-2"?><translationResults jobDate="20070123 23:20:51"... (1 Reply)
Discussion started by: purnakarthik
1 Replies
Login or Register to Ask a Question