tags:

views:

54

answers:

5

Hi, I have a file data.base which looks like:

1234 XXXX
4321 XXXX
9884 ZZZZ
5454 YYYY
4311 YYYY
9882 ZZZZ
9976 ZZZZ

( ... random occurrences like this till 10000 lines)

I would like to create a file called data.case which derives from data.base just with substitutions of XXXX, YYYY, ZZZZ for float numbers.

I wonder what would be the most compact/efficient/short way to do that on bash or friends.

What I usually do is something like:

sed -e "s/XXXX/1.34555/g" data.base > temp1
sed -e "s/YYYY/2.985/g" temp1 > temp2
sed -e "s/ZZZZ/-4.3435/g" temp2 > data.case
rm -fr temp1 temp2

But I do not think this is the most compact or efficient way when you have to deal with more than 3 substitutions.

Thanks

Thanks

+3  A: 

Use an option to ececute several commands in same sed:

sed "s/XXXX/1.34555/g; s/YYYY/2.985/g"; s/ZZZZ/-4.3435/g" data.base > data.case
dimba
A: 

sed -e "s/XXXX/1.34555/g;s/YYYY/2.985/g;s/ZZZZ/-4.3435/g"

or put them in a cmd file and list them out.

sparkkkey
A: 

Whilst sed can do multiple substitutions in one pass, the general UNIX approach which is more widely applicable and can be combined with other commands is to use command piping:

cat data.base | \
   sed -e "s/XXXX/1.34555/g" | \
   sed -e "s/YYYY/2.985/g" | \
   sed -e "s/ZZZZ/-4.3435/g" > data.base

The redirection at the end will 'unlink' the old data.base that is being used as input by cat; you could however still use a temporary file so that you can intercept error conditions and not have lost the original data.base in the process.

(When using piping, its useful to be familiar with the tee program, which saves the stream to a file whilst passing it on)

Will
its not necessary to pipe to 3 seds.
ghostdog74
+2  A: 
$ cat sedcommands
s/XXXX/1.34555/g
s/YYYY/2.985/g
s/ZZZZ/-4.3435/g
$ sed -f sedcommands data.base > data.case
Dennis Williamson
A: 

you can make use of associative arrays in awk

awk 'BEGIN{
 # add as needed
 s["XXXX"]=1.3455
 s["YYYY"]=2.985
 s["ZZZZ"]=-4.3435
}
($2 in s) {  print $1,s[$2] }' file

output

$ ./shell.sh
1234 1.3455
4321 1.3455
9884 -4.3435
5454 2.985
4311 2.985
9882 -4.3435
9976 -4.3435
ghostdog74